We’ll look at the various Python methods for reversing an array in this tutorial. The Python programming language does not support array data structures. It instead has simple-to-use built-in list structures as well as methods for performing operations.
Importing a module like Array or NumPy allows us to continue using Python’s standard Arrays. To reverse an array in Python can be achieved in three different ways.
- In Python, reversing an array list.
- In Python, reversing an array of array modules.
- In Python, reversing a NumPy array.
Reverse a List Array in Python
As previously stated, Lists and Arrays are similar in Python. The main distinction between the two is that arrays can only contain items of the same data type, whereas lists can contain items of different data types.
Because Python does not support traditional Arrays, we can use lists to represent the same thing and try to reverse them.
Let’s take a look at the various approaches we can take to complete this task.
1. Using List Slicing to Reverse an Array in Python
Using slicing methods, we can reverse a list array. In this manner, we actually generate a new list in the opposite order as the original one. Let’s look at how:
#Initializing An Array inputArray = [1,2,3,4,5] print("The Original Array Is: ",inputArray) #Reversing The Array using the Slicing Method. reversedArray = inputArray[::-1] #Printing The Reverse Array print("The Reverse Array Is: ",reversedArray)
Output:
The Original Array Is: [1, 2, 3, 4, 5]
The Reverse Array Is: [5, 4, 3, 2, 1]
2. Using Reverse() Method
Python also includes a built-in method called reverse()
, which allows you to directly reverse the order of list items right where they were originally listed.
You can use this reverse function directly on the list or array you want to reverse, and it will reverse the array in reverse order.
#Initializing An Array inputArray = [5,4,3,2,1] print("The Original Array Is: ",inputArray) #Using reverse() Function To Reverse inputArray.reverse() #Printing The Reverse Array print("The Reverse Array Is: ",inputArray)
Output:
The Original Array Is: [5, 4, 3, 2, 1]
The Reverse Array Is: [1, 2, 3, 4, 5]
3. Using Reversed Method
Another method is reversed()
, which, when passed a list, returns an iterable containing only the list’s items in reverse order.
You must pass the input array that you want to reverse to the reversed() function, which will return a reversed array or list.
And, as shown in the example code below, you must cast the returned list into a list.
#Initializing An Array inputArray = [6,7,8,9,10] print("The Original Array Is: ",inputArray) #Using reversed Method reversedArray = list(reversed(inputArray)) #Printing The Reverse Array print("The Reverse Array Is: ",reversedArray)
Output:
The Original Array Is: [6, 7, 8, 9, 10]
The Reverse Array Is: [10, 9, 8, 7, 6]
How To Reverse an Array of Array Module in Python
Despite the fact that Python does not support arrays, we can use the Array module to create array-like objects of various data types.
Despite the fact that this module places many constraints on the data type of the array, it is widely used in Python to work with array data structures.
Let’s look at how we can reverse an array created with Python’s Array module.
1. Using Reverse() Method
Reversing an array in Python is just like reversing a list; the reverse() method can be used to do so. There is no need to store the results because it reverses the array at its original location.
#Importing Array of Array Library import array #Initializing The Array inputArray = array.array('i', [4,5,6,7,8]) #Printing The Original Array print("The Original Array is: ", inputArray) #Reversing Array using Reverse Method inputArray.reverse() #Printing The Reverse Array print("The Reverse Array is: ", inputArray)
Output:
The Original Array is: array('i', [4, 5, 6, 7, 8])
The Reverse Array is: array('i', [8, 7, 6, 5, 4])
2. Using Reversed Method
When passed an array, reversed()
method returns an iterable with elements in reverse order. Take a look at the example below to see how we can use this method to reverse an array.
#Importing Array of Array Library import array #Initializing The Array inputArray = array.array('i', [6,7,8,9,10]) #Printing The Original Array print("The Original Array is: ", inputArray) #Reversing Array using Reverse Method reversedArray = array.array('i',reversed(inputArray)) #Printing The Reverse Array print("The Reverse Array is: ", reversedArray)
The Original Array is: array('i', [6, 7, 8, 9, 10])
The Reverse Array is: array('i', [10, 9, 8, 7, 6])
How To Reverse A NumPy Array In Python
The Numpy module in Python allows us to use array data structures that are extremely fast and only allow arrays of the same data type.
In this section, we will reverse an array created with the NumPy module in Python.
1. Using flip() Method
Using the NumPy module’s flip() method, you can reverse the order of a NumPy array and get back the NumPy array object.
#Importing NumPy Library import numpy as np #Initializing Array initArray = np.array(['A','B', 'C']) #Printing The Original Array print("Original Array is :",initArray) #using Flip Method To Reverse Array reversedArray = np.flip(initArray) print("The Reversed Array is: ",reversedArray)
Output:
Original Array is : ['A' 'B' 'C']
The Reversed Array is: ['C' 'B' 'A']
2. Using flipud() Method
One more method in the NumPy module is called flipud(). It turns an array up or down. It can also be used in Python to make a NumPy array that is upside down.
Let’s look at a small example to see how we can use this tool to help us.
#Importing NumPy Library import numpy as np #Initializing Array initArray = np.array(['D','E', 'F']) #Printing The Original Array print("Original Array is :",initArray) #using Flip Method To Reverse Array reversedArray = np.flipud(initArray) print("The Reversed Array is: ",reversedArray)
Original Array is : ['D' 'E' 'F']
The Reversed Array is: ['F' 'E' 'D']
3. Using Slicing Method
As we did with lists, we can use slicing to make an array in Python built with Numpy go in the opposite direction. We make a new NumPy array object that stores things in a different way.
#Importing NumPy Library import numpy as np #Initializing Array initArray = np.array(['D','E', 'F']) #Printing The Original Array print("Original Array is :",initArray) #using Flip Method To Reverse Array reversedArray = initArray[::-1] print("The Reversed Array is: ",reversedArray)
Original Array is : ['D' 'E' 'F']
The Reversed Array is: ['F' 'E' 'D']
Wrap Up
As a result of this tutorial, we learned how to reverse an array in Python using a variety of methods and techniques. Hope it has helped to clarify things for you.
Please let me know in the comments if you have a better solution or if you have anything to add to the above solution.
If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read: