Reverse an array

Using lists

Python provides an iterator to reverse an array reversed()

you can use it this way :

arr = list(range(5))
print(f'Initial array : {arr}\n')


reversed_arr = list(reversed(arr))
print(f'Reversed array : {reversed_arr}')
Initial array : [0, 1, 2, 3, 4]

Reversed array : [4, 3, 2, 1, 0]

Using numpy

If you are using numpy you can reverse an array by creating a view on the array:

import numpy as np
arr = np.arange(5)
print(f'Initial array : {arr}\n')


reversed_arr = arr[::-1]
print(f'Reversed array : {reversed_arr}')
Initial array : [0 1 2 3 4]

Reversed array : [4 3 2 1 0]