Python | Reverse array

NumPy | Python Methods and Functions | reverse array

Method # 1: Using a shortcut

# Python code for demonstration
# how to reverse the numpy array
# using the shortcut

 

import numpy as np

  
# numpy array initialization

ini_array = np.array ([ 1 , 2 , 3 , 6 , 4 , 5 ])

 
# print the initial ini_array

print ( "initial array" , str (ini_array))

 
# print type ini_array

print ( "type of ini_array" , type (ini_array))

 
# using the shortcut method for reverse

res = ini_array [:: - 1

 
# print result

print ( "final array" , str (res))

Exit:

 initial array [1 2 3 6 4 5] type of ini_array & lt; class `numpy.ndarray` & gt; final array [5 4 6 3 2 1] 

Method # 2: Using the flipud

# Python code for demonstration
# how to reverse the numpy array
# using flipud method

 

import numpy as np

 
# numpy array initialization

ini_array = np.array ([ 1 , 2 , 3 , 6 , 4 , 5 ])

 
# print the initial ini_array

print ( "initial array" , str (ini_array))

 
# print type ini_array

print ( " type of ini_array " , type (ini_array))

 
# using the flipud method to reverse

res = np.flipud (ini_array)

  
# print result

print ( "final array" , str (res))

Exit :

 initial array [1 2 3 6 4 5] type of ini_array & lt; class `numpy.ndarray` & gt; final array [5 4 6 3 2 1] 




Python | Reverse array: StackOverflow Questions

Get Solution for free from DataCamp guru