How to Reverse an Array in Python?

Python provides numerous data structures that are used to store multiple collections of data, such as list, dictionary, tuple, etc. The “list” and “array” are the frequently used data structures in Python. The array data structure stores the data of the same type while the list can store data of different data types.

There are several operations that we can perform on list and array using the built-in function and different modules. One such operation is reversing an array. To reverse an array, several methods are used in Python.

This post will present multiple methods to reverse an array in Python using numerous examples. 

Method 1: Reverse an Array Using the reverse() Method

The “reverse()” method is used in Python to reverse the list. The below code uses the “array” module and “reverse()” function to reverse the array elements:

Code:

from array import array
array_value=array('i',[15,25,33,12,5,6,78])
array_value.reverse()
print("Reversed Array: ",array_value)

In the above code:

  • The “array” module is imported into the program.
  • The “array()” function accepts the type code “i” as a first argument and array value as a second argument. The array() function returns the array with integer point array items.
  • The “reverse()” function reverses the given array.

Output:

The input array has been successfully reversed using the reverse() function.

Method 2: Reverse an Array Using reversed() Method

The “reversed()” method can be utilized to reverse the list or array in Python. The below code uses the “reversed()” method to reverse the given array:

Code:

from array import array
array_value=array('i',[5,2,4,6,78,5,2])
reversed_array=array('i',reversed(array_value))
print("Reversed Array: ",reversed_array)

In the above code:

  • The “array()” function of the array module creates the array named “array_value”.
  • The “reversed()” method accepts the array value as an argument and returns the reversed object.
  • The reversed object is then passed as a parameter to the “array()” function that will create the reversed array.

Output:

The input array has been reversed.

Method 3: Reverse a Numpy Array Using the flip() Method 

The Numpy module provides a “flip()” method that is used to reverse a numpy array by preserving the array shape. The following code reversed the array via the flip() function:

Code:

import numpy
array_value=numpy.array([45,6,23,43,65,12])
output=numpy.flip(array_value)
print("Reversed Array:",output)

In the above code:

  • The “numpy.array()” function is utilized to create the array.
  • The “numpy.flip()” function takes the array as an argument and reverses the order of the NumPy array elements.

Output:

The given numpy array elements have been reversed.

Method 4: Reverse a Numpy Array Using Slicing Method

The “slicing” method is normally used with the string to return the specific range of characters. The below code uses this method to reverse a Numpy array:

Code:

import numpy
array_value=numpy.array([45,6,23,43,65,12])
output=array_value[::-1]
print("Reversed Array:",output)

In the above code:

  • The “numpy.array()” function of the array module generates the array.
  • The “slicing” method is used to reverse the order of items of the input array by taking the negative value “-1”.

Output:

The given numpy array has been successfully reversed using the slicing method.

Method 5: Reverse a List Array Using List Slicing

The below code uses the Python “slicing” method to reverse the list array:

Code:

array_value=[5,10,15,20,25,30,35]
output=array_value[::-1]
print("Reversed Array:",output)

In the above code:

  • The dynamic list array is initialized by enclosing the element value inside the square bracket.
  • The “slicing” method uses the expression “[::-1]” to reverse the input list array.

Output:

The input list array has been successfully reversed.

Method 6: Reverse a List Array Using reverse() Method

The built-in“reverse()” method is used in the below code to reverse the order of list array element:

Code:

array_value=[5,25,50,100,52,6]
array_value.reverse()
print("Reversed Array:",array_value)

In the above code:

  • The “reverse()” method reverses the input list array elements.
  • The print method prints the reversed array.

Output:

The given list array has been reversed.

Conclusion

To reverse an array the “reverse()” method, “reversed()” method, “flip()” method, and “Slicing” method are used in Python. The arrays can be generated in Python by using a simple list initializer, using the numpy module function, and using the array module function. Using numerous examples, this post explained how to reverse an array in Python.