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 NumPy reverse array in Python.
# Below are the quick examples
# Example 1: Use reverse a numpy array with the basic slicing method
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:reverse a numpy array using numpy.flip() function
arr2 = np.flip(arr)
# Example 4: using reverse() function
arr= array.array('i',[2, 5, 8, 14, 25])
arr.reverse()
# Example 5: Use np.fliplr() function
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
We can use the basic slicing method to reverse a NumPy array. Use this syntax [::-1]
as the index of the array to reverse it, and will return a new NumPy array object which holds items in a reversed order. Let’s create a NumPy array using numpy.array()
and reverse it.
import numpy as np
# Create an 1D input array
arr = np.array([2, 5, 8, 14, 25])
# reverse numpy array use slicing method
arr2 = arr[::-1]
print(arr2)
# Output
# [25 14 8 5 2]
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.
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 :
Input array.
It returns flipped array in up-down direction.
3.2 flipud() Example to Reverse 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(arr2)
Yields the same output as above.
4. Use flip() Function to Reverse a Array
Python np.flip()
function is another method to reverse the order of array elements along the given axis. The shape of the array is preserved, but the elements are reordered.
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:
Input array.axis:
Axis or axes along which to flip over. The default, axis=None, will flip over all of the axes of the input array. If axis is negative it counts from the last to the first axis.
4.3 flip() Example
Let’s reverse an array by using np.flip() function. It returns reversed array with shape preserved.
# Reverse a numpy array using numpy.flip() function
arr2 = np.flip(arr)
print(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 2-D 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
By using this fliplr() method let’s reverse the two-dimensional numpy array. This function takes 2-D array as input and returns the array in the same shape but by reversing the order of elements for each dimension.
# Create a 2D input array
arr= np.array([[2, 5, 8, 14, 19,25],
[3, 6, 9, 13, 18, 29]])
# Use np.fliplr() function
arr2 = np.fliplr(arr)
print(arr2)
# Output
# [[25 19 14 8 5 2]
# [29 18 13 9 6 3]]
6. Use reverse() Function to Reverse the Array Element
We can also use reverse()
function, returns an iterable with elements in reverse order.
import array
# Create an input array
arr= array.array('i',[2, 5, 8, 14, 25])
# using reverse() function
arr.reverse()
print(arr)
# Output
# array('i', [25, 14, 8, 5, 2])
7. 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!!
Related Articles
- Gets round values of NumPy array
- How to create NumPy one’s array
- How to use median() in NumPy
- How to transpose() NumPy Array in Python
- How to Calculate minimum() of Array in NumPy
- Python NumPy zeros() Function
- How to use Python NumPy arange() Function
- Python NumPy repeat() Function