The Python programming directly does not support the array data structure. For that, we use the in-built list data structure. But sometimes, we need to use the array in Python programming, and for that, we have to import the module Numpy.
So, this article about reversing a list is divided into two concepts as follows:
- Methods to reverse a list
- Methods to reverse a Numpy Array
Methods to reverse a list in Python:
1. Using reverse () method:
Python programming also provides some built-in methods like C++ and other programming languages, which we can use directly according to our requirements. The reverse () is a python built-in method, and we can directly reverse a list in place. The main drawback of this is it will work on the original list, which means the original list will be reversed.
The syntax of the reverse in-built method is:
The reverse method does not accept any parameters.
In Cell number [1]: We created a list with the name of the city. Then we call the in-built method reverse () as said in the syntax, and then we again print the list city. The result shows that the list is now reversed.
In-place methods have some advantages and some disadvantages. The main advantage of the in-place method is that it does not require much extra memory for the shuffling. But the main drawback is that it works with the original list only.
2. Using Reverse Iterator with the reversed() Function
The other built-in method to reverse a list is reversed (). This method is similar to the reverse (), but the only difference is that it takes a list as an argument and does not destroy the original list. This method also does not work like in-place as a reverse () method, and neither it creates a copy of the elements.
The reversed () method takes a list as a parameter and returns it as an iterable object having elements in reverse order. If only we want to print the elements in the reversed order, then this method is swift.
The syntax to use the reversed () method is:
In cell number [7]: We created a list with the name of the items. Then we passed that list to the reversed () method and iterate over the list items. We can see that the value starts printing from the last element first, then the second-last one, and so on.
In cell number [8]: We again print our original list to confirm either our original list (items) was destroyed or not. So from the results, ensure that the original list was not destroyed by the reversed () method.
If we want to convert the iterable object into a list, then we have to use the list () method around the iterable object, as shown below. This will give us the new list with the reverse elements.
3. Using the slicing method
Python programming has one extra feature, which we called slicing. The slicing is the extension of the square brackets feature. This slicing helps us to access the particular elements which we required. But through this slicing, we can also reverse a list using the notation [: : -1].
In cell number [10]: We created a list with the name of the items. We then applied the slicing notation on the list (items) and got the results in the reverse order. This slicing also does not destroy the original list as the cell number [11] shows the original list still exists.
Reversing a list using slicing is slow compared to the in-place methods because it has created a shallow copy of all elements and needs enough memory to complete the process.
4. Method: Using the range function
We can also use the range function to reverse a list. This method is just a custom method and not built-in, as we discussed before. This function basically plays with the index value of the items in the list and prints the value as shown below. So, these types of functions depend upon the user’s skills and how they designed the custom code.
The main reason to add the above custom code using the range function is to tell the users they can design different kinds of methods according to their requirements.
Methods to reverse a Numpy Array:
1. Method: Using the flip () method
The flip () method is a numpy built-in function that helps us reverse a numpy array quickly. This method does not destroy the original numpy array, as shown below:
In cell number [34]: We import the NumPy library package.
In cell number [35]: We created a NumPy array with the name of new_array. Then we print the new_array.
In cell number [36]: We called the flip built-in function and passed the new_array, which we just created in cell number [35] as a parameter. Then we print the rev_array, and from the results, we can say that the flip () method reverses the elements of the NumPy array.
In cell number [37]: We print the original array to confirm either the original NumPy array exists or is destroyed by the flip () method. We found from the results that flip () does not change the original NumPy array.
2. Method: Using the flipud () method
Another method we will use to reverse the Nnumpy array elements is the flipud () method. This flipud () is basically used for the up/down the array elements. But we can also use this method to reverse a numpy array as shown below:
In cell number [47]: We created a NumPy array with the name of new_array. Then we print the new_array.
In cell number [48]: We called the flipud built-in function and passed the new_array, which we just created in cell number [47] as a parameter. Then we print the rev_array, and from the results, we can say that the flipud () method reverses the elements of the NumPy array.
In cell number [49]: We print the original array to confirm either the original NumPy array exists or is destroyed by the flipud () method. We found from the results that flipud () does not change the original NumPy array.
3. Method: Using the slicing method
In cell number [46]: We created a NumPy array with the name of new_array. Then we print the new_array.
In cell number [50]: We then applied the slicing notation on the numpy array and got the results in the reverse order. Then we print the rev_array, and from the results, we can say that the slicing method reverses the elements of the NumPy array.
In cell number [51]: We print the original array to confirm either the original NumPy array exists or is destroyed by the slicing method. We found from the results that slicing does not change the original NumPy array.
Conclusion:
In this article, we have studied different methods to reverse a list array and NumPnumpy array. We have also seen how the reverse sometimes works in place like the reverse () method. We have also seen some advantages and disadvantages of in-place (like reverse () method) and without in-place (like reversed () method). We mostly focus on the built-in methods as custom methods depend upon the user’s knowledge skills.
from Linux Hint https://ift.tt/3dR6bmJ
0 Comments