In this post, we will see a different ways to reverse array in Python.
[toc]
Let’s see the example first.
Example:
Input : [10 20 30 40 50 60] Output : [60 50 40 30 20 10]
As we know, Python language has not come up with an array data structure. So in python there are some ways to create an array like data structure. Some ways like: use list, use array module and use numpy module for array.
Using list
Now first, we will see different ways to reverse a list in Python.
You can use the list as array in python and perform various operations on the list to reverse it.
Using a concept of list slicing.
Here, we are using concept of Python list slicing to reverse a list. List slicing performs in-place reversal , so no extra space is required.
Below is the Python code given:
# list
arr = [10, 20, 30, 40, 50, 60]
print("Original array is:", arr)
# reverse the list using list slicing
rev_arr = arr[: : -1]
print("Reversed array is:", rev_arr)
Output:
Original array is: [10, 20, 30, 40, 50, 60] Reversed array is: [60, 50, 40, 30, 20, 10]
Using a reverse() method of list.
Here, we are using reverse() method of list to reverse a list. reverse() method performs in-place reversal , so no extra space is required.
Below is the Python code given:
# list
arr = [10, 20, 30, 40, 50, 60]
print("Original array is:", arr)
# reverse the list using reverse() method
arr.reverse()
print("Reversed array is:", arr)
Output:
Original array is: [10, 20, 30, 40, 50, 60] Reversed array is: [60, 50, 40, 30, 20, 10]
Using a reversed() built-in function.
Here, we are using reversed() function to reverse a list. This function returns reversed iterator object , so we convert it into list by using list() function.
Below is the Python code given:
# list
arr = [10, 20, 30, 40, 50, 60]
print("Original array is:", arr)
# reverse the list using reversed() function
rev_arr = list(reversed(arr))
print("Reversed array is:", rev_arr)
Output:
Original array is: [10, 20, 30, 40, 50, 60] Reversed array is: [60, 50, 40, 30, 20, 10]
Using array modile
Now, we will see different ways to reverse an array using array module in Python.
Using a reverse() method of array object.
Here, we are using reverse() method of list to reverse an array. reversed() method performs in-place reversal , so no extra space is required.
Below is the Python code given:
# import array module
import array
# integer array
arr = array.array('i',[10, 20, 30, 40, 50, 60])
print("Original array is:", arr)
# reverse an array using reverse() method
arr.reverse()
print("Reversed array is:", arr)
Output:
Original array is: array('i', [10, 20, 30, 40, 50, 60])
Reversed array is: array('i', [60, 50, 40, 30, 20, 10])
Using a reversed() built-in function.
Here, we are using reversed() function to reverse an array. This function returns reversed iterator object ,so we convert it into array by using array.array() function.
Below is the Python code given:
# import array module
import array
# integer array
arr = array.array('i', [10, 20, 30, 40, 50, 60])
print("Original array is:", arr)
# reverse an array using reversed() function
rev_arr = array.array('i', reversed(arr))
print("Reversed array is:", rev_arr)
Output:
Original array is: array('i', [10, 20, 30, 40, 50, 60])
Reversed array is: array('i', [60, 50, 40, 30, 20, 10])
Using numpy array
Now, we will see a different ways to reverse a numpy array in Python.
Using a flip() method of numpy module.
Here, we are using flip() method of numpy to reverse a numpy array. `flip() method returns numpy array object.
Below is the Python code given:
# import numpy module
import numpy as np
# numpy array
arr = np.array([10, 20, 30, 40, 50, 60])
print("Original array is:", arr)
# reverse an array using flip() method
# 0 denotes horizonatl axis
rev_arr = np.flip(arr, 0)
print("Reversed array is:", rev_arr)
Output:
Original array is: [10 20 30 40 50 60] Reversed array is: [60 50 40 30 20 10]
Using a concept of array slicing.
Here, we are using concept of slicing to reverse a numpy array. slicing performs in-place reversal , so no extra space is required.
Below is the Python code given:
# import numpy module
import numpy as np
# numpy array
arr = np.array([10, 20, 30, 40, 50, 60])
print("Original array is:", arr)
# reverse an array using slicing
rev_arr = arr[: : -1]
print("Reversed array is:", rev_arr)
Output:
Original array is: [10 20 30 40 50 60] Reversed array is: [60 50 40 30 20 10]
Using a flipud() method of numpy module.
Here, we are using flipud() method of numpy to reverse a numpy array. flipud method returns numpy array object.
Below is the Python code given:
# import numpy module
import numpy as np
# numpy array
arr = np.array([10, 20, 30, 40, 50, 60])
print("Original array is:", arr)
# reverse an array using slicing
rev_arr = np.flipud(arr)
print("Reversed array is:", rev_arr)
Output:
Original array is: [10 20 30 40 50 60] Reversed array is: [60 50 40 30 20 10]
Print array in reverse order
You can loop through the array in reverse order. Loop will start from array.lenght -1 and end at 0 by step value of 1.
#Initialize array
arrInt = [1, 2, 3, 4, 5];
print("Original array: ");
for i in range(0, len(arrInt)):
print(arrInt[i],end=' '),
print("\nArray in reverse order: ");
# Print array in reverse order
for i in range(len(arrInt)-1, -1, -1):
print(arrInt[i],end=' ')
Output:
Original array: 1 2 3 4 5 Array in reverse order: 5 4 3 2 1
That’s all about how to reverse array in Python.