Reverse an Array in Python – 10 Examples

Reversing Arrays In Python

Introduction

In this tutorial, we’ll go over the different methods to reverse an array in Python. The Python language does not come with array data structure support. Instead, it has in-built list structures that are easy to use as well as provide some methods to perform operations.

We can continue to use the typical Arrays in Python by import a module like Array or NumPy. Our tutorial is going to be divided into three parts, each dealing with reversing individual Array types in Python. They are,

  • Reversing an Array List in Python,
  • Reversing an Array of Array Module in Python,
  • Reversing a NumPy Array in Python.

Now let us get right into the topic.

Reverse a List Array in Python

As we already discussed Lists and Arrays are similar in Python. Where the major difference among the two is that arrays only allow items of the same data type whereas lists allow them to be different.

Since Python doesn’t support conventional Arrays, we can use lists to depict the same and try to reverse them. Let us take a look at the different methods following which we can accomplish this task.

1. Using List Slicing to Reverse an Array in Python

We can reverse a list array using slicing methods. In this way, we actually create a new list in the reverse order as that of the original one. Let us see how:

#The original array
arr = [11, 22, 33, 44, 55]
print("Array is :",arr)

res = arr[::-1] #reversing using list slicing
print("Resultant new reversed array:",res)

Output:

Array is : [1, 2, 3, 4, 5]
Resultant new reversed array: [5, 4, 3, 2, 1]

2. Using reverse() Method

Python also provides a built-in method reverse() that directly reverses the order of list items right at the original place.

Note: In this way, we change the order of the actual list. Hence, the original order is lost.

#The original array
arr = [11, 22, 33, 44, 55]
print("Before reversal Array is :",arr)

arr.reverse() #reversing using reverse()
print("After reversing Array:",arr)

Output:

Before reversal Array is : [11, 22, 33, 44, 55]
After reversing Array: [55, 44, 33, 22, 11]

3. Using reversed() Method

We have yet another method, reversed() which when passed with a list returns an iterable having just items of the list in reverse order. If we use the list() method on this iterable object, we get a new list which contains our reversed array.

#The original array
arr = [12, 34, 56, 78]
print("Original Array is :",arr)
#reversing using reversed()
result=list(reversed(arr))
print("Resultant new reversed Array:",result)

Output:

Original Array is : [12, 34, 56, 78]
Resultant new reversed Array: [78, 56, 34, 12]

Reversing an Array of Array Module in Python

Even though Python doesn’t support arrays, we can use the Array module to create array-like objects of different data types. Though this module enforces a lot of restrictions when it comes to the array’s data type, it is widely used to work with array data structures in Python.

Now, let us see how we can reverse an array in Python created with the Array module.

1. Using reverse() Method

Similar to lists, the reverse() method can also be used to directly reverse an array in Python of the Array module. It reverses an array at its original location, hence doesn’t require extra space for storing the results.

import array

#The original array
new_arr=array.array('i',[2,4,6,8,10,12])
print("Original Array is :",new_arr)

#reversing using reverse()
new_arr.reverse()
print("Reversed Array:",new_arr)

Output:

Original Array is : array('i', [2, 4, 6, 8, 10, 12])
Resultant new reversed Array: array('i', [12, 10, 8, 6, 4, 2])

2. Using reversed() Method

Again, the reversed() method when passed with an array, returns an iterable with elements in reverse order. Look at the example below, it shows how we can reverse an array using this method.

import array

#The original array
new_arr=array.array('i',[10,20,30,40])
print("Original Array is :",new_arr)

#reversing using reversed()
res_arr=array.array('i',reversed(new_arr))
print("Resultant Reversed Array:",res_arr)

Output:

Original Array is : array('i', [10, 20, 30, 40])
Resultant Reversed Array: array('i', [40, 30, 20, 10])

Reversing a NumPy Array in Python

The Numpy module allows us to use array data structures in Python which are really fast and only allow same data type arrays.

Here, we are going to reverse an array in Python built with the NumPy module.

1. Using flip() Method

The flip() method in the NumPy module reverses the order of a NumPy array and returns the NumPy array object.

import numpy as np

#The original NumPy array
new_arr=np.array(['A','s','k','P','y','t','h','o','n'])
print("Original Array is :",new_arr)

#reversing using flip() Method
res_arr=np.flip(new_arr)
print("Resultant Reversed Array:",res_arr)

Output:

Original Array is : ['A' 's' 'k' 'P' 'y' 't' 'h' 'o' 'n']
Resultant Reversed Array: ['n' 'o' 'h' 't' 'y' 'P' 'k' 's' 'A']

2. Using flipud() Method

The flipud() method is yet another method in the NumPy module which flips an array up/down. It can also be used to reverse a NumPy array in Python. Let us see how we can use it in a small example.

import numpy as np

#The original NumPy array
new_arr=np.array(['A','s','k','P','y','t','h','o','n'])
print("Original Array is :",new_arr)

#reversing using flipud() Method
res_arr=np.flipud(new_arr)
print("Resultant Reversed Array:",res_arr)

Output:

Original Array is : ['A' 's' 'k' 'P' 'y' 't' 'h' 'o' 'n']
Resultant Reversed Array: ['n' 'o' 'h' 't' 'y' 'P' 'k' 's' 'A']

3. Using Simple Slicing

As we did earlier with lists, we can reverse an array in Python built with Numpy using slicing. We create a new NumPy array object which holds items in a reversed order.

import numpy as np

#The original NumPy array
new_arr=np.array([1,3,5,7,9])
print("Original Array is :",new_arr)

#reversing using array slicing
res_arr=new_arr[::-1]
print("Resultant Reversed Array:",res_arr)

Output:

Original Array is : [1 3 5 7 9]
Resultant Reversed Array: [9 7 5 3 1]

Conclusion

So, in this tutorial, we learned how we can reverse an array in Python using various methods or techniques. Hope it gives a clear understanding.

References