Reversing Arrays in Python: Methods and Techniques
Learn how to reverse arrays in Python using various methods such as slicing, reverse()
, reversed()
, and for
loops. Explore practical techniques for rearranging array elements in reverse order with examples and code snippets.
Python - Reverse Arrays
Reversing an array means rearranging the array elements in the opposite order. There are various methods to reverse an array in Python, including reverse()
and reversed()
methods.
Although array is not a built-in data type in Python, the array module helps create a homogeneous collection of string, 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 extracts a part of the array within the specified indices. Using the slice operation in the form [::-1]
will create a new array by reversing the original one.
Example
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)
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
The reverse()
method of the list class can reverse the sequence of numbers in an array. To use it with an array created through the array module, first convert the array to a list, call the reverse()
method, and convert the list back to an array.
Example
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 a 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)
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 returns an iterator that displays array elements in reverse order.
Example
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)
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 for loop, traverse the elements of the original array in reverse order and append each element to a new array.
Example
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)
Output
array('i', [10, 5, 15, 4, 6, 20, 9])
array('i', [9, 20, 6, 4, 15, 5, 10])