Skip to content
Related Articles

Related Articles

Improve Article

Python | Reverse a numpy array

  • Last Updated : 21 Feb, 2019

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course




# 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))

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))

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :