Computer >> Computer tutorials >  >> Programming >> Python

Python program to print the elements of an array in reverse order


When it is required to print the elements of an array in reverse order, the list can be iterated over from the end.

Below is a demonstration of the same −

Example

my_list = [21, 32, 43, 54, 75]
print("The list is : ")
for i in range(0, len(my_list)):
   print(my_list[i])
print("The list after reversal is : ")
for i in range(len(my_list)-1, -1, -1):
   print(my_list[i])

Output

The list is :
21
32
43
54
75
The list after reversal is :
75
54
43
32
21

Explanation

  • A list is defined, and is displayed on the console.

  • The list is iterated over, and displayed.

  • It is reversed by iterating it from the last element.

  • Every element is displayed on the console.