How to Reverse a NumPy Array

by | Programming, Python, Tips

This tutorial will go through how to reverse a NumPy array using slicing, flipud(), fliplr(), and flip() with code examples.


Reverse NumPy Array using Slicing

The simplest way to reverse a NumPy array is to use list slicing. The syntax for slicing is [start:end:step]. We can use the step value to return a copy of the array in the reverse order. Let’s look at an example:

import numpy as np

arr = np.array([1, 3, 5, 7, 9, 11])

res = arr[::-1]

print(f'Original array: {arr}')
print(f'Reverse array: {res}')

Let’s run the code to see the result:

Original array: [ 1  3  5  7  9 11]
Reverse array: [11  9  7  5  3  1]

Reverse Multidimensional NumPy Array using Slicing

We can apply list slicing to reverse multidimensional NumPy arrays. Let’s look at an example:

import numpy as np

arr = np.array([[2, 3, 4], [1, 3, 5], [8, 1, 9]])
res = arr[::-1]

print(f'Original array: {arr}')
print(f'Reverse array: {res}')

Let’s run the code to get the result:

Original array: [[2 3 4]
 [1 3 5]
 [8 1 9]]
Reverse array: [[8 1 9]
 [1 3 5]
 [2 3 4]]

Reverse NumPy Array using numpy.flipud()

NumPy has a built-in method flipud(), which reverses the order of elements along axis 0 (up/down). This method requires the array to be at least one-dimensional. Let’s look at an example with a one-dimensional array:

import numpy as np

arr = np.array([1, 3, 5, 7, 9, 11])

res = np.flipud(arr)

print(f'Original array: {arr}')
print(f'Reverse array: {res}')

Let’s run the code to get the result:

Original array: [ 1  3  5  7  9 11]
Reverse array: [11  9  7  5  3  1]

Reverse Multi-dimensional NumPy Array using numpy.flipud()

The flipud() method is equivalent to arr[::-1, ...]. Let’s look at an example of the flipud() method with a two-dimensional array.

import numpy as np

arr = np.array([[2, 3, 4], [1, 3, 5], [8, 1, 9]])
res = np.flipud(arr)

print(f'Original array: {arr}')
print(f'Reverse array: {res}')

Let’s run the code to see the result:

Original array: [[2 3 4]
 [1 3 5]
 [8 1 9]]
Reverse array: [[8 1 9]
 [1 3 5]
 [2 3 4]]

Reverse Multi-dimensional NumPy Array using numpy.fliplr()

NumPy has a built-in method fliplr(), which reverses the order of elements along axis 1 (left/right). This method requires the array to be at least two-dimensional. For a two-dimensional array, the method flips the entries in each row in the left/right direction, while preserving the columns. Let’s look at an example with a two-dimensional array:

import numpy as np

arr = np.array([[2, 3, 4], [1, 3, 5], [8, 1, 9]])
res = np.fliplr(arr)

print(f'Original array: {arr}')
print(f'Reverse array: {res}')

Let’s run the code to see the result:

Original array: [[2 3 4]
 [1 3 5]
 [8 1 9]]
Reverse array: [[4 3 2]
 [5 3 1]
 [9 1 8]]

Reverse NumPy Array using numpy.flip()

NumPy has a built-in method flip(), which reverses the order of elements in an array along the given axis. This method requires the array to be at least one-dimensional.

The syntax for flip() is as follows:

numpy.flip(m,axis=None)

Parameters

m: Required. Input array.

axis: Optional. Axis or axes along which to flip over. Default is None, which will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis. If axis is a tuple of ints, then perform flipping on all of the axes in the tuple.

It follows that flip(m, 0) is equivalent to flipud(m), and flip(m, 1) is equivalent to fliplr(m).

Let’s look at an example with a one-dimensional array:

import numpy as np

arr = np.array([1, 3, 5, 7, 9, 11])

res = np.flip(arr)

print(f'Original array: {arr}')

print(f'Reverse array: {res}')

Let’s run the code to see the result:

Original array: [ 1  3  5  7  9 11]
Reverse array: [11  9  7  5  3  1]

Reverse Multi-dimensional NumPy Array using numpy.flip()

Let’s look at an example of using numpy.flip() with a two-dimensional array:

import numpy as np

arr = np.array([[2, 3, 4], [1, 3, 5], [8, 1, 9]])

res = np.flip(arr)

print(f'Original array: {arr}')
print(f'Reverse array: {res}')

Let’s run the code to get the result:

Original array: [[2 3 4]
 [1 3 5]
 [8 1 9]]
Reverse array: [[9 1 8]
 [5 3 1]
 [4 3 2]]

Let’s look at an example of flip() on a three-dimensional NumPy array.

import numpy as np

arr = np.array([[[178, 189, 567], [145, 239, 445], [197, 345, 678]],
                [[56, 78, 190], [46, 10, 11], [6, 2, 1]],
                [[45, 118, 203], [72, 119, 34], [87, 9, 5]]])
res = np.flip(arr, axis=2)

print(f'Original array: {arr}')
print(f'Reverse array: {res}')

Let’s run the code to see the result:

Original array: [[[178 189 567]
  [145 239 445]
  [197 345 678]]

 [[ 56  78 190]
  [ 46  10  11]
  [  6   2   1]]

 [[ 45 118 203]
  [ 72 119  34]
  [ 87   9   5]]]
Reverse array: [[[567 189 178]
  [445 239 145]
  [678 345 197]]

 [[190  78  56]
  [ 11  10  46]
  [  1   2   6]]

 [[203 118  45]
  [ 34 119  72]
  [  5   9  87]]]

Summary

Congratulations on reading to the end of this tutorial! We have gone through the various ways to reverse a NumPy array. The simplest way to reverse an array is to use list slicing, for example, arr[::-1].

For further reading on NumPy methods, go to the article: How-to Guide for Python NumPy Where Function

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee