Reversing an array is the operation of rearranging the array elements in the opposite order. There are various methods and approaches to reverse an array in Python including reverse() and reversed() methods.
In Python, is not one of the built-in . However, Python”s standard library has array module which helps us to create a homogenous collection of , integer or float types.
Ways to Reverse an Array in Python
To reverse an array, use the following approaches −
- Using slicing operation
- Using reverse() method
- Using reversed() method
- Using for loop
Using slicing operation
Slicing operation is the process of extracting a part of array within the specified indices. In Python, if we use the slice operation in the form [::-1] then, it will display a new array by reversing the original one.
In this process, the interpreter starts from the end and stepping backwards by 1 until it reaches the beginning of the array. As a result, we get a reverse copy of original array.
Example
The below example demonstrates how to use the slicing operation to reverse an array in Python.
import array as arr # creating array numericArray = arr.array(''i'', [88, 99, 77, 55, 66]) print("Original array:", numericArray) revArray = numericArray[::-1] print("Reversed array:",revArray)
When you run the code, it will produce the following output −
Original array: array(''i'', [88, 99, 77, 55, 66]) Reversed array: array(''i'', [66, 55, 77, 99, 88])
Reverse an Array Using reverse() Method
We can also reverse the sequence of numbers in an array using the of list class. Here, is a built-in type in Python.
Since reverse() is a method of list class, we cannot directly use it to reverse an array created through the Python array module. We have to first transfer the contents of an array to a list with tolist() method of array class, then we call the reverse() method and at the end, when we convert the list back to an array, we get the array with reversed order.
Example
Here, we will see the use of reverse() method in reversing an array in Python.
import array as arr # creating an array numericArray = arr.array(''i'', [10,5,15,4,6,20,9]) print("Array before reversing:", numericArray) # converting the array into list newArray = numericArray.tolist() # reversing the list newArray.reverse() # creating a new array from reversed list revArray = arr.array(''i'', newArray) print ("Array after reversing:",revArray)
It will produce the following output −
Array before reversing: array(''i'', [10, 5, 15, 4, 6, 20, 9]) Array after reversing: array(''i'', [9, 20, 6, 4, 15, 5, 10])
Reverse an Array Using reversed() Method
The reversed() method is another way to reverse elements of an array. It accepts an array as a parameter value and returns an iterator object that dispalys array elements in reverse order.
Example
In this example, we are using the reversed() method to reverse an array in Python.
import array as arr # creating an array numericArray = arr.array(''i'', [12, 10, 14, 16, 20, 18]) print("Array before reversing:", numericArray) # reversing the array newArray = list(reversed(numericArray)) # creating a new array from reversed list revArray = arr.array(''i'', newArray) print ("Array after reversing:",revArray)
On executing the above code, it will display the following output −
Array before reversing: array(''i'', [12, 10, 14, 16, 20, 18]) Array after reversing: array(''i'', [18, 20, 16, 14, 10, 12])
Using for Loop
To reverse an array using a , we first traverse the elements of the original array in reverse order and then append each element to a new array.
Example
The following example shows how to reverse an array in Python using for loop.
import array as arr a = arr.array(''i'', [10,5,15,4,6,20,9]) b = arr.array(''i'') for i in range(len(a)-1, -1, -1): b.append(a[i]) print(a) print(b)
It will produce the following output −
array(''i'', [10, 5, 15, 4, 6, 20, 9]) array(''i'', [9, 20, 6, 4, 15, 5, 10])