Python NumPy Reverse Array

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:November 16, 2023
  • Reading time:16 mins read

NumPy reverse array is used to return a new array with the contents of the original array in reverse order. There are various ways to create or initialize reverse arrays in NumPy, for example by using slicing, numpy.flipud(), numpy.flip(), numpy.fliplr(), and reverse() functions. In this article, I will explain how to use Python reverse array in different ways with examples.

1. Quick Examples of Reverse Array

If you are in a hurry, below are some quick examples of the NumPy reverse array in Python.


# Below are the quick examples

# Example 1: Reverse numpy array 
# Using slicing notation
arr = np.array([2, 5, 8, 14, 25])
arr2 = arr[::-1]

# Example 2: Use flipud() function 
# To reverse a numpy array
arr2 = np.flipud(arr)
    
# Example 3: Using numpy.flip() function 
# Reverse a numpy array 
arr2 = np.flip(arr)
    
# Example 4: Using reverse() function
arr= array.array('i',[2, 5, 8, 14, 25])
arr.reverse()
    
# Example 5: Using fliplr() function
# Flip the 2D array horizontally 
arr= np.array([[2, 5, 8, 14, 19,25],
               [3, 6, 9, 13, 18, 29]])
arr2 = np.fliplr(arr)

2. Reverse NumPy Array Using Basic Slicing Method

You can reverse a NumPy array using basic slicing notation. For instance, arr[::-1] uses slicing notation to create a view of the array with a step of -1, effectively reversing the order of elements in the array. This approach is concise and works well for one-dimensional arrays.


# Import numpy
import numpy as np

# Create an 1D input array  
arr = np.array([2, 5, 8, 14, 25])
print("Original array:", arr)

# Reverse numpy array 
# Using slicing notation
arr2 = arr[::-1]
print("Reversed array:",arr2)

Yields below output.

numpy reverse array

3. Use flipud() Function to Reverse a Array

The numpy.flipud() function is used to reverse the order or flip the array(entries in each column) in up-down direction. This function preserved the shape of the NumPy array.

The numpy.flipud() function specifically flips the array along its first axis, which corresponds to the up-down direction. It maintains the shape of the array while reversing the order of elements along that axis. This function is particularly useful when you want to reverse the order of rows in a 2D array, effectively flipping it vertically.

3.1 Syntax of NumPy flipud()

Following is the syntax of the numpy.flipud() function.


# Syntax of flipud()
numpy.flipud(arr)

Following are the parameters of the flipud() function.

  • arr : The input array to be reversed.

It returns a flipped array in the up-down direction.

3.2 flipud() Example to Reverse Array

The numpy.flipud() function is used to reverse the order of elements along the first axis (up-down direction) of the array. Let’s take the same array used in the above example and flip it using flipud() function.


# Use flipud() function 
# To reverse a numpy array
arr2 = np.flipud(arr)
print("Reversed array:",arr2)

Yields the same output as above.

4. Use flip() Function to Reverse a Array

The np.flip() function in NumPy is a versatile method to reverse the order of array elements along a specified axis. It preserves the shape of the array while reordering the elements. This function is useful when you need to reverse arrays along a specific axis or when dealing with multidimensional arrays.

4.1 Syntax of NumPy flip()

Following is the syntax of the np.flip() function.


# Syntax of flip()
numpy.flip(arr, axis=None)

4.2 Parameters of flip()

Following are the parameters of the flip() function.

  • arr: The input array to be flipped.
  • axis: The axis or axes along which the array is flipped. If axis is not specified, the array is flattened before flipping. Default is None. If the axis is negative it counts from the last to the first axis.

4.3 flip() Example

The numpy.flip() function is a more general-purpose function for flipping elements along specified axes.

In the below example, you can use the numpy.flip() function to reverse the order of elements in the 1D array. The numpy.flip() function is a concise way to reverse the order of elements in an array.


# Using numpy.flip() function 
# Reverse a numpy array 
arr2 = np.flip(arr)
print("Flipped array:",arr2)

Yields the same output as above.

5. Use numpy.fliplr() Function

The numpy.fliplr() function is used to reverse the order of elements for a 2D array. This flip array entry in each row in the left-right direction. columns are preserved but appear in a different order than before.

5.1 Syntax of NumPy fliplr()

Following is the syntax of the fliplr() function.


# Syntax of fliplr()
numpy.fliplr(arr)

5.2 Parameters of fliplr()

  • arr: Input array, It must be in 2-D dimensions.

5.3 fliplr() Example to Reverse Array

The numpy.fliplr() function is used to flip an array horizontally (left to right). It reverses the order of columns along the second axis (axis 1).

In the below example, np.fliplr(arr) flips the 2D array arr horizontally, reversing the order of columns along the second axis. This function is particularly useful when you want to reverse the order of columns in a 2D array.


# Create a 2D input array  
arr= np.array([[2, 5, 8, 14, 19,25],[3, 6, 9, 13, 18, 29]])
print("Original 2D array:\n", arr)

# Using fliplr() function
# Flip the 2D array horizontally 
arr2 = np.fliplr(arr)
print("Flipped 2D array horizontally:\n",arr2)

# Output:
# Original 2D array:
# [[ 2  5  8 14 19 25]
# [ 3  6  9 13 18 29]]
# Flipped 2D array horizontally:
# [[25 19 14  8  5  2]
# [29 18 13  9  6  3]]

6. Use the reverse() Function to Reverse the Array Element

You can also use reverse() function, it returns an iterable with elements in reverse order. Keep in mind that the reverse() method is specific to Python’s built-in array module. If you were using NumPy arrays, you would use slicing or the numpy.flip() function as demonstrated in the previous examples.


import array

# Create an input array  
arr = array.array('i',[2, 5, 8, 14, 25])
print("Original array:\n", arr)

# Using reverse() function
arr.reverse()
print("Reversed array:\n",arr)

# Output:
# Original array:
#  array('i', [2, 5, 8, 14, 25])
# Reversed array:
#  array('i', [25, 14, 8, 5, 2])

Frequently Asked Questions

How do I reverse a NumPy array?

You can reverse a NumPy array in several ways. Two common methods are using numpy.flip() and using slicing notation.

Can I reverse a NumPy array in place?

You can reverse a NumPy array in place using slicing notation. For example, arr[::-1] creates a reversed view of the array, and by assigning it back to arr[:], you modify the original array in place.

What happens if I use numpy.flip() with a multi-dimensional array?

When you use numpy.flip() with a multi-dimensional array, it reverses the elements along the specified axis. The axis parameter in numpy.flip() determines along which axis or axes you want to reverse the array.

Does reversing a NumPy array change the original array?

It depends on how you perform the reversal. If you use slicing notation or numpy.flip() and assign the result to a new variable, the original array remains unchanged. If you want to reverse the array in place, you need to use slicing notation directly on the original array.

Can I reverse only a part of a NumPy array?

You can reverse only a specific part of a NumPy array using slicing notation. Specify the range you want to reverse within the square brackets. For example, arr[1:4] selects elements from index 1 to 3, and arr[1:4][::-1] creates a reversed view of that subarray. By assigning this reversed subarray back to the original array, you reverse only the specified part in place.

Conclusion

In this article, I have explained how to use Python NumPy reverse array in different ways with examples. Also learned how to initialize a reverse array by using NumPy slicing, flipud(), flip(), fliplr(), and reverse() function.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply