Python | Reverse a numpy array
As we know Numpy is a general-purpose array-processing package which provides a high-performance multidimensional array object, and tools for working with these arrays. Let’s discuss how can we reverse a numpy array.
Method #1: Using shortcut Method
# Python code to demonstrate # how to reverse numpy array # using shortcut method 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)) |
chevron_right
filter_none
Output:
initial array [1 2 3 6 4 5] type of ini_array <class 'numpy.ndarray'> final array [5 4 6 3 2 1]
Method #2: Using flipud function
# Python code to demonstrate # how to reverse numpy array # using flipud method 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)) |
chevron_right
filter_none
Output:
initial array [1 2 3 6 4 5] type of ini_array <class 'numpy.ndarray'> final array [5 4 6 3 2 1]
Recommended Posts:
- Python | Flatten a 2d numpy array into 1d array
- Python | Multiply 2d numpy array corresponding to 1d array
- Python | Reverse an array upto a given position
- Python Slicing | Reverse an array in groups of given size
- Python | Replace negative value with zero in numpy array
- Python | Ways to add row/columns in numpy array
- Python | Find Mean of a List of Numpy Array
- Python | Filter out integers from float numpy array
- Python | dtype object length of Numpy array of strings
- Python | Numpy numpy.ndarray.__le__()
- Python | Numpy numpy.ndarray.__gt__()
- Python | Numpy numpy.ndarray.__floordiv__()
- Python | Numpy numpy.ndarray.__lt__()
- Python | Numpy numpy.ndarray.__mul__()
- Python | Numpy numpy.ndarray.__pos__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



