Reversing a 1D and 2D numpy array using np.flip() and [] operator in Python.
Reverse 1D Numpy array using ‘[]’ operator :
By not passing any start or end parameter, so by default complete array is picked. And as step size is -1, so elements selected from last to first.
# Program : import numpy as sc # Creating a numpy array num_arr = sc.array([11,22,33,44,55,66]) print('Original Array: ',num_arr) # To get reverse of numpy array rev_arr = num_arr[::-1] print('Reversed Array : ', rev_arr)
Output : Original Array:Â [11 22 33 44 55 66]Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Reversed Array :Â [66 55 44 33 22 11]
Reverse Array is View Only :
Here if we do any modification in reversed array, it will also be reflected in original array.
# Program : import numpy as sc # Creatin a numpy array num_arr = sc.array([11,22,33,44,55,66]) # To get reverse of numpy array rev_arr = num_arr[::-1] rev_arr[4]=63 print('Modified reversed Array : ', rev_arr) print('Original array is: ',num_arr)
Output : Modified reversed Array :Â [66 55 44 33 63 11]Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Original array is:Â [11 63 33 44 55 66]
Reverse Numpy array using np.flip() :
flip()
function provided by Python’s numpy module helps to flip or reverse the content of numpy array.
Syntax : numpy.flip(arr, axis=None)
where,
- arr :Â A numpy array
- axis : Axis along which contents need to be flipped. If
None
, contents will be flipped along axis of array.
Reverse 1D Numpy array using np.flip() :
Here as it is 1 -D Numpy array, there is no need of axis parameter.
# Program : import numpy as sc # To create a Numpy array num_arr = sc.array([11,22,33,44,55]) print('Original array: ',num_arr) # To get reverse of numpy array rev_arr = sc.flip(num_arr) print('Reversed Array is : ', rev_arr)
Output : Original array:Â [11 22 33 44 55] Reversed Array is :Â [55 44 33 22 11]
Reverse 2D Numpy Array using np.flip() :
Reverse contents in all rows and all columns of 2D Numpy Array :
Here we don’t provide parameter in np.flip()
function, then contents will be reversed along the axes of 2-D Numpy array.
# Program : import numpy as sc # to create a 2D Numpy array twoD_Arr = sc.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('Original Array is: ',twoD_Arr) # to reverse 2D numpy array rev_Arr = sc.flip(twoD_Arr) print('Reversed Array : ') print(rev_Arr)
Output : Original Array is: [[1 2 3] [4 5 6] [7 8 9]] Reversed Array : [[9 8 7] [6 5 4] [3 2 1]]
Reverse contents of all rows only in 2D Numpy Array :
If we provide axix parameter i.e. axis=1
, then rows of array will be reversed.
# Program : import numpy as sc # to create a 2D Numpy array twoD_Arr = sc.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('Original Array is: ',twoD_Arr) # to reverse the content of each row in array rev_Arr = sc.flip(twoD_Arr, axis=1) print('Reversed Array : ') print(rev_Arr)
Output : Original Array is:Â [[1 2 3] [4 5 6] [7 8 9]] Reversed Array : [[3 2 1] [6 5 4] [9 8 7]]
Reverse contents of all columns only in 2D Numpy Array :
If we provide axix parameter i.e. axis=0
, then rows of array will be reversed.
# Program : import numpy as sc # to create a 2D Numpy array twoD_Arr = sc.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('Original Array is: ',twoD_Arr) # to reverse the content of each column in array rev_Arr = sc.flip(twoD_Arr, axis=0) print('Reversed Array : ') print(rev_Arr)
Output : Original Array is: [[1 2 3] [4 5 6] [7 8 9]] Reversed Array : [[7 8 9] [4 5 6] [1 2 3]]
Reverse contents of only one row in 2D Numpy Array :
Let, we want to reverse only 1st row of a Numpy array.
# Program : import numpy as sc # to create a 2D Numpy array twoD_Arr = sc.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('Original Array is: ',twoD_Arr) # to reverse only 1st row twoD_Arr[0] = sc.flip(twoD_Arr[0]) print('Reversed Array : ') print(twoD_Arr)
Output : Original Array is: [[1 2 3] [4 5 6] [7 8 9]] Reversed Array : [[3 2 1] [4 5 6] [7 8 9]]
Reverse contents of only one column in 2D Numpy Array :
Let, we want to reverse only 3rd column of a Numpy array.
# Program : import numpy as sc # to create a 2D Numpy array twoD_Arr = sc.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print('Original Array is: ',twoD_Arr) # to reverse the content of 3rd column in array twoD_Arr[:,2] = sc.flip(twoD_Arr[:,2]) print('Reversed Array : ') print(twoD_Arr)
Output : Original Array is: [[1 2 3] [4 5 6] [7 8 9]] Reversed Array : [[1 2 9] [4 5 6] [7 8 3]]