In this Python tutorial, we will see, how to reverse numpy array in Python. There are various ways we can reverse python numpy array.
There are multiple ways 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
How to Reverse Numpy Array in Python
One common task when working with arrays is to reverse the order of the elements in a Python array. There are several methods to reverse a numpy array in Python, depending on the shape and dimensions of the array.
Method-1: Reverse numpy array using numpy.flip() function
The first method uses the numpy.flip() function to reverse the order of the elements in the array in Python. This function returns a new array with the elements flipped in the specified axis.
#Importing the NumPy library
import numpy as np
#Creating a NumPy array
arr = np.array([9, 8, 3, 6, 2, 1])
#Reversing the NumPy array using the flip() method
result = np.flip(arr)
#Printing the reverse array
print("Reverse array:", result)
The above code imports the NumPy library and creates a one-dimensional array of integers with the values [9, 8, 3, 6, 2, 1] using the np.array() function.
- The np.flip() function is then used to reverse the order of the elements in the array. The reversed array is stored in the variable result, which is then printed to the console.
This is how to reverse numpy array.
Method-2: Reverse numpy array in python using array slicing
The second method uses array slicing to reverse the order of the elements in the array. This method creates a new array that is a view of the original array with the elements reversed.
# Import numpy library
import numpy as np
# Create an array of integers
arr= np.array([1, 2, 3, 6, 4, 5])
# Reverse the order of the array elements using slicing
result = arr[::-1]
# Print the reversed array
print("Reverse array", result)
The above code first imports the NumPy library as np. Then, it creates a NumPy array “arr” consisting of the elements [1, 2, 3, 6, 4, 5].
- Next, it reverses the array using the slicing operator “:” and the step size “-1”, which means that the array will be reversed. The reversed array is stored in the variable “result”.
- Finally, the program prints the reversed array to the console using the “print” statement.
Output: Reverse array [5 4 6 3 2 1]
This is how to reverse numpy array in python.
Method-3: Reverse numpy array using reverse() function
We can also use the reverse() function to reverse numpy array in python.
# Import the array module
import array
# Create an integer array with values
arr = array.array('i', [4, 5, 9, 1, 9, 3])
# Print the original array
print(arr)
# Reverse the array in place using the reverse() method
arr.reverse()
# Print the reversed array
print("Reversed Array:", arr)
The above code imports the array module and creates an integer array arr containing the elements [4,5,9,1,9,3] using the array() function.
- Then it prints the original array using the
print()
function. - Next, the reverse() method is used to reverse the elements of the array in-place, meaning that the original array is modified. Finally, the reversed array is printed to the console using the print() function.
Output: array('i', [4, 5, 9, 1, 9, 3])
Reversed Array: array('i', [3, 9, 1, 9, 5, 4])
This is another way to reverse numpy array using the numpy reverse function.
Method-4: Reverse Python numpy array Using flipud() method
This method uses the numpy.flipud() function to flip the order of the rows in the input 2D array. This function returns a new array with the rows flipped vertically. This method is similar to Method 5 but flips the rows instead of the columns.
# Importing the NumPy library
import numpy as np
# Creating a NumPy array
arr = np.array([9, 8, 3, 6, 2, 1])
# Reversing the order of elements of the array using flipud() method
result = np.flipud(arr)
# Printing the reversed array
print("Reversed array: ", result)
The code imports the NumPy library as ‘np’. Then it creates a NumPy array ‘arr’ with the values [9, 8, 3, 6, 2, 1].
- The code uses the NumPy function ‘flipud’ to reverse the array along the vertical axis, which is equivalent to flipping the array upside down. The result is assigned to the variable ‘result’. Finally, the code prints the reversed array using the ‘print’ function.
Output: Reversed array: [1 2 6 3 8 9]
This is how to reverse numpy array in python.
Method-5: Reverse numpy array in python using fliplr() function
This method uses the numpy.fliplr() function to reverse the order of the columns in a 2D array in Python. This function returns a new array with the columns flipped horizontally.
# Import numpy module
import numpy as np
# Create a NumPy array
arr = np.array([[3, 5, 6, 7, 2, 1], [2, 5, 6, 7, 8, 9]])
# Reverse the order of columns in the NumPy array using fliplr() function
result = np.fliplr(arr)
# Print the result
print("Reverse array", result)
The above code begins by importing the NumPy library and assigning it the alias “np”. Then, a two-dimensional NumPy array is created using the np.array() method, with two rows and six columns.
- Next, the np.fliplr() function is used to flip the two-dimensional array horizontally, effectively reversing the order of the columns. This flipped array is assigned to a new variable called result.
- Finally, the print() function is used to display the flipped array on the console.
Output: Reverse array [[1 2 7 6 5 3]
[9 8 7 6 5 2]]
This is an example of reverse numpy array in Python.
Method-6: Using the numpy.ndarray.flatten() method
This method uses the numpy.ndarray.flatten() method to flatten the array into a 1D array, then uses array slicing to reverse the order of the elements in the flattened array, and finally uses the numpy.ndarray.reshape() method to reshape the reversed array to the original shape of the input array.
# Import the numpy library and alias it as 'np'
import numpy as np
# Define a 1D numpy array called 'arr' with the values [1, 2, 3, 4, 5]
arr = np.array([1, 2, 3, 4, 5])
# Use the flatten() method to convert the 1D array to a flat 1D array,
# then use slicing to reverse the order of the elements,
# then use the reshape() method to convert the flat array back into a 1D array with the same shape as the original array
reversed_arr = arr.flatten()[::-1].reshape(arr.shape)
# Print the reversed array
print(reversed_arr)
The above code imports the numpy library and aliases it as “np”. It then defines a 1D numpy array called “arr” with the values [1, 2, 3, 4, 5].
- The code then uses the flatten() method to convert the 1D array to a flat 1D array, the slicing operation [::-1] to reverse the order of the elements, and the reshape() method to convert the flat array back into a 1D array with the same shape as the original array.
- Finally, the code prints the reversed array to the console. The output of the print statement will be the reversed array [5, 4, 3, 2, 1].
Output: [5 4 3 2 1]
This is another way to reverse numpy array.
In this Python tutorial, we have covered how to reverse numpy array using the following methods:
- Using the numpy.flip() function
- Using array slicing
- Using reverse() function
- Using flipud() method
- Using fliplr() function
- Using the numpy.ndarray.flatten() method
You may also like the following tutorials:
- How to Convert NumPy Array to List in Python
- Python NumPy Median() Function
- Numpy Divide in Python
- Python Copy NumPy Array
- Python NumPy genfromtxt()
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.