NumPy Reverse Array in Python [6 Methods]

In this Python tutorial, I will show how NumPy reverse array in Python using various ways with some examples.

To reverse an array in Python using NumPy, various methods such as np.flip(), array slicing, and np.ndarray.flatten() can be employed. np.flip() reverses elements along a specified axis, array slicing offers a simple syntax for reversing, while flipud() and fliplr() flip arrays vertically and horizontally, respectively. The reverse() function can be used for lists after converting the array into a list.

NumPy Reverse Array in Python Methods

There are six different methods to reverse the NumPy array in Python, which are shown below:

  • Using the numpy.flip() function
  • Using array slicing
  • Using reverse() function
  • Using flipud() method
  • Using fliplr() function
  • Using the numpy.ndarray.flatten() method

Let’s see them one by one using some illustrative examples:

1. NumPy reverse array using np.flip() function

The np.flip() function is one of the most straightforward ways NumPy reserve array in Python. It reverses the order of elements in an array along the specified axis. If the axis is not specified, it reverses the array along all axes.

For instance:

import numpy as np

temps_nyc = np.array([58, 60, 62, 63, 61, 59, 57])
reversed_temps = np.flip(temps_nyc)
print(reversed_temps)

Output:

[57 59 61 63 62 60 58]

The output from running the code in PyCharm is visually represented in the screenshot below.

reverse np array in Python

2. How to reverse a NumPy array using array slicing

Array slicing is a Python technique that can be used by NumPy reserve array in Python. It doesn’t require any special function and is quite intuitive.

READ:  Python Django concatenate string

For example:

import numpy as np

scores = np.array([24, 30, 28, 35])
reversed_scores = scores[::-1]
print(reversed_scores)

Output:

[35 28 30 24]

Below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

NumPy reverse array in Python

3. NumPy array reverse using reverse() function

The reverse() function is a Python list method and not directly applicable to NumPy arrays, we’ll first convert the NumPy array to a list, apply the reverse() function, and then convert it back to a NumPy array.

import numpy as np

rainfall = np.array([36.2, 34.5, 39.2, 42.8, 48.3])
rainfall_list = rainfall.tolist()
rainfall_list.reverse()
reversed_rainfall = np.array(rainfall_list)
print(reversed_rainfall)

Output:

[48.3 42.8 39.2 34.5 36.2]

The following screenshot illustrates the results of executing the code in the PyCharm editor.

reverse a numpy array in Python

4. Reverse array Python using flipud() method

The flipud() function is used to flip arrays vertically (up/down). Ans this is how NumPy reverse array in Python uses flipud() function.

import numpy as np

states = np.array([["Illinois", "Indiana"], ["Wisconsin", "Michigan"]])
flipped_states = np.flipud(states)
print(flipped_states)

Output:

[['Wisconsin' 'Michigan']
 ['Illinois' 'Indiana']]

After executing the code in Pycharm, one can see the output in the below screenshot.

python reverse array

5. NumPy array reverse Python using fliplr() function

The fliplr() function flips arrays horizontally (left/right). And this is how NumPy reverse array in Python uses the fliplr() function.

import numpy as np

votes = np.array([[300, 250], [400, 350]])
flipped_votes = np.fliplr(votes)
print(flipped_votes)

Output:

[[250 300]
 [350 400]]

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

reverse array in numpy in Python

6. Reverse NumPy array using the numpy.ndarray.flatten() method

While np.ndarray.flatten() does not directly reverse an array, it can be used in conjunction with array slicing to flatten and then reverse a multi-dimensional array.

import numpy as np

elevations = np.array([[200, 300], [400, 500]])
reversed_elevations = elevations.flatten()[::-1]
print(reversed_elevations)

Output:

[500 400 300 200]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

Python program to print the elements of an array in reverse order

Conclusion

Understanding how NumPy reverse array in Python using six different methods like np.flip(), array slicing, flipud(), fliplr(), reverse() after list conversion, and the numpy.ndarray.flatten() method provides the flexibility and efficiency needed for effective data manipulation in Python.

READ:  Matplotlib two y axes

The choice of the method depends upon the problem we are dealing with.

You may also like the following tutorials: