Write a Python program to print array elements in reverse order. In this example, the list slicing with negative value prints the numpy array elements in reverse order.
import numpy as np arr = np.array([15, 25, 35, 45, 55, 65, 75]) print('Printing the Array Elements in Reverse') print(arr[::-1]) print('\nPrinting the Array Elements') print(arr)
In this Python program, the for loop iterates array elements from last to first based on the index position and print the array items in reverse order.
import numpy as np arr = np.array([15, 25, 35, 45, 55, 65, 75]) for i in range(len(arr) - 1, -1, -1): print(arr[i], end = ' ')
75 65 55 45 35 25 15
This Python program helps to print array elements in reverse order using a while loop.
import numpy as np arr = np.array([22, 44, 66, 77, 89, 11, 19]) i = len(arr) - 1 while i >= 0: print(arr[i], end = ' ') i = i - 1
19 11 89 77 66 44 22
This example allows entering the Numpy Array elements and printing them in reverse order.
import numpy as np arrlist1 = [] arrTot = int(input("Total Number of Array Elements to enter = ")) for i in range(1, arrTot + 1): arrvalue = int(input("Please enter the %d Array Value = " %i)) arrlist1.append(arrvalue) arr = np.array(arrlist1) print('Printing the Array Elements in Reverse') for i in range(len(arr) - 1, -1, -1): print(arr[i], end = ' ')
Total Number of Array Elements to enter = 9
Please enter the 1 Array Value = 21
Please enter the 2 Array Value = 34
Please enter the 3 Array Value = 65
Please enter the 4 Array Value = 78
Please enter the 5 Array Value = 98
Please enter the 6 Array Value = 34
Please enter the 7 Array Value = 65
Please enter the 8 Array Value = 87
Please enter the 9 Array Value = 99
Printing the Array Elements in Reverse
99 87 65 34 98 78 65 34 21