Reversing an array means changing the order of the elements. The first element becomes the last, the second becomes the second-to-last, and so on, and finally, the last becomes the first.
For example, if an array has “n” elements, then the element at index i moves to index n-1-i. So, if an array has [1, 2, 3, 4] elements, the reversed array becomes [4, 3, 2, 1]. You can see that it is a mirror image of the original array.
Here are the ways to reverse a Numpy array:
- Using slicing (Efficient way)
- Using numpy.flip()
- Using numpy.flipud()
Method 1: Using slicing arr[::-1]
The slicing arr[::-1] is the most efficient way to create a reverse array because you create the view into an original array. You can modify the original array; the view will update to reflect the changes.
import numpy as np arr = np.arange(5) print(arr) print('After reversing the array using [::-1]: ') print(arr[::-1]) # [0 1 2 3 4] # After reversing the array using [::-1]: # [4 3 2 1 0]
The arr[::-1] just returns the reversed view. Slicing does not create a new array in memory and it avoids overhead copying the data. This is the reason this approach is blazing fast.
Pros
- It is short, concise, and highly efficient.
- The syntax is readable and does not require to know any type of function.
Cons
- One caveat in this approach is that if the array is not contiguous in memory (e.g., due to striding or other advanced slicing), a copy might be created.
- It does not clearly send a message to the reader that we are reversing the array.
Method 2: Using numpy.flip()
The numpy.flip() method reverses the order of elements in an array along the given axis and creates a new array in memory.
import numpy as np main_arr = np.array([11, 21, 19, 18]) print(main_arr) reversed_arr = np.flip(main_arr) print('After reversing the array using np.flip(): ') print(reversed_arr) # [11 21 19 18] # After reversing the array using np.flip(): # [18 19 21 11]
Here, you can see from the output that we get a new reversed array with flipped elements.
Pros
- Its intent is clear to reverse an array.
- It provides an “axis” parameter through which you can reverse the array along any axis for multi-dimensional arrays.
Cons
- It creates a new array in memory which is less efficient than slicing for larger arrays.
Method 3: Using numpy.flipud()
The numpy.flipud() method helps reverse the numpy array along axis 0 (up/down).
import numpy as np main_arr = np.array([11, 21, 19, 18]) print(main_arr) reversed_arr = np.flipud(main_arr) print('After reversing the array using np.flipud(): ') print(reversed_arr) # [11 21 19 18] # After reversing the array using np.flipud(): # [18 19 21 11]
Pros
- It is more readable than the np.flip() method and is specifically designed for flipping arrays along the vertical axis (axis 0).
Cons
- Similar to np.flip(), it also creates a new copy in memory. So, less efficient than slicing.
- It only works for flipping along the vertical axis.
Reversing Two-dimensional numpy array
To reverse the two-dimensional array from left to right, you can use the “np.fliplr()” method.
import numpy as np main_arr_2d = np.array([[11, 21], [19, 18]]) print(main_arr_2d) reversed_2d_arr = np.fliplr(main_arr_2d) print('After reversing 2D array using np.fliplr(): ') print(reversed_2d_arr) # [[11 21] # [19 18]] # After reversing 2D array using np.fliplr(): # [[21 11] # [18 19]]
Reversing multi-dimensional numpy array
We will reverse a multi-dimensional array along with a specific axis.
import numpy as np main_multi_arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # 3D array # Reverse along axis 0 reversed_axis0 = main_multi_arr[::-1, :, :] reversed_axis0_flip = np.flip(main_multi_arr, axis=0) # Reverse along axis 1 reversed_axis1 = main_multi_arr[:, ::-1, :] reversed_axis1_flip = np.flip(main_multi_arr, axis=1) # Reverse along both axis 0 and 2 reversed_both = main_multi_arr[::-1, :, ::-1] reversed_both_flip = np.flip(main_multi_arr, axis=(0, 2)) print("Original array:\n", main_multi_arr) print("\nReversed along axis 0:\n", reversed_axis0) print("\nReversed along axis 1:\n", reversed_axis1) print("\nReversed along both axis 0 and 2:\n", reversed_both)
Output
That’s all!