In this article, we will learn different ways to reverse an array in python.
Reversing an array is one of the basic functions we should learn while working with arrays.
However, in python, we do not have any in-built array
data structure. So we have to use in-built python modules like Numpy and Array to declare an array in python.
In Python, we use List
data structure to store multiple elements of different data types in a single variable.
So, here in this article, we will learn:
- Reversing an List array in Python
- Declare an Array using numpy and array module and reversing the array.
Reverse a List in Python
Since in Python, we don’t have an array as a data structure we can use a list instead. Let’s see some examples to reverse a list array below. To reverse a list we can use:
- List slicing
- reverse() method
- reversed() method
Reverse a List array using list slicing
We can reverse a list using the list-slicing method in python. It creates a new list with the extracted items from a list.
Syntax:
list[start:end:step]
The start is the index from where we want to start slicing and the end is the end index where we want it to stop and the step is the increment number by which we want to slice. By default, it’s 1.
Now, to reverse the order of elements in a list we can use a negative step value (-1
).
Example:
my_list = [1,2,3,4,5]
rev_list = my_list[::-1]
print(rev_list)
Output:
[5, 4, 3, 2, 1]
When the step is negative it starts to extract and create a new list in reverse order.
Reversing a List array using reverse() method
In Python, we have an in-built method called reverse()
, which can reverse the order of the elements in the list.
The reverse()
method modifies the original list.
Example:
my_list = [1,2,3,4,5]
my_list.reverse()
print(my_list)
Output:
[5, 4, 3, 2, 1]
Reverse a List array using the reversed() function
We can also use the reversed()
function to reverse a list array in python.
The reversed()
is an in-built python function that returns a reversed iterator object of a given sequence.
Syntax:
reversed(seqn)
We can pass the list as a parameter in the reversed()
function to return the elements in reverse order.
We have to wrap the returned iterable object in a list()
to get the output as a list.
Example:
my_list = [1,2,3,4,5]
rev_list = list(reversed(my_list))
print(rev_list)
Output:
[5, 4, 3, 2, 1]
Reverse an array using Array Module in Python
In Python, we can also use an array data-structure too, but for that, we have to use modules like Array. The Array
modules in Python let us create an array-like data structure and work on it.
To declare an array using this module we have to follow this syntax:
array.array(typecode, [sequence])
To declare an array with array
module, we have to pass the typecode and the sequence.
The typecode
lets us specify the data type of the array. Here, we will use integers in our examples, so we will write i
.
If you use the Array module, we can use these two methods to reverse the order of the elements:
- reverse() method
- reversed() function
Using reverse() method
The reverse()
method works similarly to that we have used with the Python List above.
However, here we will use it with an array. And to declare an array in python we have to use the array
module by importing it into our program.
Example:
import array as ar
arr = ar.array('i', [1,2,3,4,5])
arr.reverse()
print(arr)
Output:
array('i', [5, 4, 3, 2, 1])
Using reversed() method
We can also use the reversed()
function just like the list data type for the array of array module.
We have to first declare the array and pass the array as a parameter in the reversed()
function. It will return an iterable object with the elements in reverse order in a newly created array.
Example:
import array as ar
arr = ar.array('i', [1,2,3,4,5])
rev_arr = ar.array('i',reversed(arr))
print(rev_arr)
Output:
array('i', [5, 4, 3, 2, 1])
Reverse Numpy Array in Python
We can also create an array data structure using the Numpy
module in python.
To declare an array using numpy we use this syntax:
numpy.array(arrObj)
The different methods provided by Numpy module to reverse an array are:
- flip() method
- slicing method
Let’s see each method with an example:
Using flip() method to reverse numpy array
The numpy flip()
method reverses the order of the given array and returns it.
Example:
import numpy as np
arr = np.array([1,2,3,4,5])
rev_arr = np.flip(arr)
print(rev_arr)
Output:
[5, 4, 3, 2, 1]
Reverse a numpy array using list slicing
Just like we did with the python List example above, we can also reverse a numpy array using the list slicing method.
Example:
import numpy as np
arr = np.array([1,2,3,4,5])
rev_arr = arr[::-1]
print(rev_arr)
Output:
[5, 4, 3, 2, 1]
Here, we have declared the array using np.array()
method, and then using the general list slicing method we have reversed the order of the elements in the given array.
Conclusion:
Here, we have learned how to reverse the order of a python List first and learned how to declare an array using Array
and Numpy
modules and then reverse the order of elements using those module’s methods.
Other Articles You’ll Also Like:
How to flatten nested list in python (5 ways)
How to split a list into multiple list using python
Prepend List in Python (Append at the beginning)