You are currently viewing Reverse an Array in Python

How to reverse an array in Python? You can reverse an array using various approaches of Python. Reversing means the order of the elements in an array should be started from the last. Python does not have a built-in data Type of array. You can represent a list as an array. An array is a collection of elements of the same data type. Python provides an array module for defining arrays of specified datatypes. You can use array methods to perform various operations of arrays.

Advertisements

In this article, I will explain how to reverse the array using lists, array module, and NumPy module with multiple examples.

Following are the ways to reverse an array.

  • Using Python list
  • Using Array Module
  • Using NumPy module

1. Quick Examples of Reversing an Array

If you are in a hurry, below are some quick examples of reversing an array.


# Below are some quick examples reversing an array
import array as arr
import numpy as np

# Initialize the array
arr = [1, 3, 6, 9, 12]

# Example 1: Using list slicing
rev_arr = arr[::-1]

# Example 2: Using for loop
for i in range(len(arr)-1, -1, -1):   

# Example 3: Using reverse() function
arr.reverse()

# Example 4: Using reversed() function
rev_arr = list(reversed(arr))

# Example 5: Uisng array module of reverse()
arr1 = arr.array('i', [1, 3, 6, 9, 12])
arr1.reverse()

# Example 6: Using array module of reversed()
arr1 = arr.array('i', [1, 3, 6, 9, 12])
arr_rev = arr.array('i', reversed(arr1))

# Example 7: Reverse numpy array using slicing method
arr = np.array([1, 3, 6, 9, 12])
arr1 = arr[::-1]

# Example 8: Reverse a numpy array using numpy.flip() function 
arr1 = np.flip(arr)

# Example 9: Use flipud() function to reverse a numpy array
arr1 = np.flipud(arr)

1. Using a List to Reverse a Python Array

In Python, lists are one of the most common data structures that is mutable and doesn’t have a fixed size. It can be used to store elements of different data types. Whereas Python arrays allow elements of the same data type. Due to the usage of less memory arrays are faster than the lists.

1.1 Reverse an Array using List-slicing

To reverse an array use list slicing. Slicing is a technique using which you can select a particular portion of an array. In this example, arr[::-1] is used to create a new array with reversed elements which is a copy of the original array. The -1 step value indicates slicing starts from the end and moves towards the beginning, effectively reversing the order of elements.


# Reverse an array using list slicing
# Initialize the array
arr = [1, 3, 6, 9, 12]
print("Given array is:", arr)
rev_arr = arr[::-1]
print( "After reversing an array:", rev_arr)

Yields the below output.

python array reverse

1.2 Reverse an Array using the For Loop in Python

You can use a for loop to iterate the given array in reversing order. In general, the range() function can generate the sequence of numbers, if you set the step param with a negative value, it can generate a sequence of decreasing numbers.

To iterate range(len(arr)-1, -1, -1) use for loop, it will return an array of elements in the reversing order.

Related: In Python, you can use for loop to iterate iterable objects in reversing order and get them in backwards directions.


# Reverse an array using for loop
# Initialize the array
arr = [1, 3, 6, 9, 12]
print("Given array is:", arr)
print("Reversing an array using for loop")
for i in range(len(arr)-1, -1, -1):     
    print(arr[i]) 

Yields the below output.

Python reverse array

1.3 Using reverse() Method

You can reverse the elements of an array in Python using the built-in method list.reverse(), which reverses the array in place.


# Reverse the array using reverse() function
# Initialize the array
arr = [1, 3, 6, 9, 12]
print("Given array is:", arr) 
arr.reverse()
print("After reversing an array:", arr)

# Output:
# Given array is: [1, 3, 6, 9, 12]
# After reversing an array: [12, 9, 6, 3, 1]

1.4 Using reversed() Method

To reverse an array use reversed() function. When we pass an array into this function it can return an iterable having items of the array in reverse order. Then apply the list() method on this iterable object, you can get a new list that contains our reversed array.


# Reverse the array using reversed() function
# Initialize the array
arr = [1, 3, 6, 9, 12]
print("Given array is:", arr) 
rev_arr = list(reversed(arr))
print("After reversing an array:", rev_arr)

# Output:
# Given array is: [1, 3, 6, 9, 12]
# After reversing an array: [12, 9, 6, 3, 1]

2. Reverse an Array using an Array Module of Python

Similarly, you can use the array module of Python to reverse an array. Python doesn’t support arrays, you can use this module to create an array of specified datatypes and perform various operations over the arrays using its methods.

2.1 Using reverse() Method

To reverse an array use the reverse() method of the array module. It reverses the elements of an array in place same as like lists. Let’s apply the reverse() method on the given array, to update the given array by reversing its elements.


import array as arr
arr1 = arr.array('i', [1, 3, 6, 9, 12])
print("Given array is:", arr1) 
arr1.reverse()
print("After reversing an array:", arr1)

# Output:
# Given array is: array('i', [1, 3, 6, 9, 12])
# After reversing an array: array('i', [12, 9, 6, 3, 1])

2.2 Using reversed() Method

You can use reversed() method which is another method of array module, to reverse an array. Simply, pass the given array into this function, and it will return the reversed array which is the copy of the original array.


import array as arr
arr1 = arr.array('i', [1, 3, 6, 9, 12])
print("Given array is:", arr1) 
arr_rev = arr.array('i', reversed(arr1))
print("After reversing an array:", arr_rev)

# Output:
# Given array is: array('i', [1, 3, 6, 9, 12])
# After reversing an array: array('i', [12, 9, 6, 3, 1]) 

3. Reverse an Array using NumPy in Python

Alternatively, You can use NumPy to reverse an array. Numpy is a Python library used for working with arrays. This library provides a numpy module for implementing arrays by using its various methods.

3.1 Reverse NumPy Array Using Slicing Method

You can use the basic slicing method to reverse a NumPy array. Use this syntax [::-1] as the index of the array to reverse it, and will return a new NumPy array object which holds items in a reversed order.

Note: Before applying the NumPy function we need to import the numpy module as np.

Let’s create a NumPy array using numpy.array() and reverse it.


import numpy as np

# Create an 1D input array  
arr = np.array([1, 3, 6, 9, 12])
print("Given array:", arr)

# Reverse numpy array using slicing method
arr1 = arr[::-1]
print("After reversing an array:", arr1)

# Output:
# Given array: [ 1  3  6  9 12]
# After reversing an array: [12  9  6  3  1]

3.2 Use the flip() Function to Reverse an Array

Python np.flip() function is a method of numpy module to reverse the order of array elements along the given axis. The shape of the array is preserved, but the elements are reordered.


import numpy as np

# Create an 1D input array  
arr = np.array([1, 3, 6, 9, 12])
print("Given array:", arr)

# Reverse a numpy array using numpy.flip() function 
arr1 = np.flip(arr)
print("After reversing an array:", arr1)

# Output: 
# Given array: [ 1  3  6  9 12]
# After reversing an array: [12  9  6  3  1]

3.3 Use flipud() Function to Reverse an Array

The numpy.flipud() function is used to reverse the order or flip the array(entries in each column) in the up-down direction. This function preserved the shape of the NumPy array.


import numpy as np

# Create an 1D input array  
arr = np.array([1, 3, 6, 9, 12])
print("Given array:", arr)

# Use flipud() function to reverse a numpy array
arr1 = np.flipud(arr)
print("After reversing an array:", arr1)

# Output: 
# Given array: [ 1  3  6  9 12]
# After reversing an array: [12  9  6  3  1]

4. Conclusion

In this article, I will explain how to reverse an array using multiple ways of Python like lists, array modules, and numpy modules with multiple examples.

Happy learning!!