The Wayback Machine - https://web.archive.org/web/20231124193857/https://www.geeksforgeeks.org/python-reverse-a-numpy-array/
Open In App
Related Articles

Python | Reverse a numpy array

Improve Article
Improve
Save Article
Save
Like Article
Like

As we know Numpy is a general-purpose array-processing package that provides a high-performance multidimensional array object, and tools for working with these arrays. Let’s discuss how can we reverse a Numpy array. 

Using flip() function to Reverse a Numpy array

The numpy.flip() function reverses the order of array elements along the specified axis, preserving the shape of the array.

Python3




import numpy as np
 
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
 
# using shortcut method to reverse
res = np.flip(ini_array)
 
# printing result
print("final array", str(res))


Output:

final array [5 4 6 3 2 1]

Using the list slicing method to reverse a Numpy array

This method makes a copy of the list instead of sorting it in order. To accommodate all of the current components, making a clone requires additional room. More RAM is used up in this way. Here, we’re utilizing Python’s slicing method to invert our list.

Python3




import numpy as np
 
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
 
# printing initial ini_array
print("initial array", str(ini_array))
 
# printing type of ini_array
print("type of ini_array", type(ini_array))
 
# using shortcut method to reverse
res = ini_array[::-1]
 
# printing result
print("final array", str(res))


Output:

initial array [1 2 3 6 4 5]
type of ini_array <class 'numpy.ndarray'>
final array [5 4 6 3 2 1]

Using flipud function to Reverse a Numpy array 

The numpy.flipud() function flips the array(entries in each column) in up-down direction, shape preserved.

Python3




import numpy as np
 
# initialising numpy array
ini_array = np.array([1, 2, 3, 6, 4, 5])
 
# printing initial ini_array
print("initial array", str(ini_array))
 
# printing type of ini_array
print("type of ini_array", type(ini_array))
 
# using flipud method to reverse
res = np.flipud(ini_array)
 
# printing result
print("final array", str(res))


Output:

initial array [1 2 3 6 4 5]
type of ini_array <class 'numpy.ndarray'>
final array [5 4 6 3 2 1]

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 16 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials