Python Code for Reverse an Array

Python code fo reverse an array

Reverse an Array or List in Python

Here in this program, we will learn how to reverse an array using Python concept. Reverse an array means the last element should be displayed first, followed by second last element and so on.In Python there are 3 ways to reverse an array such:

1) Reversing a list in-place with the list.reverse() method
2) Using the “[::-1]” list slicing trick to create a reversed copy
3) Creating a reverse iterator with the reversed() built-in function

Here we applied list slicing trick to reverse an array
For Example:
Enter the size of array: 6
Enter the elements: 8
4
91
34
10
7
Reversed array is: 7
10
34
91
4
8

Algorithm for reverse an array using Python

STEP 1: Declare and initialize an array.
STEP 2: Iterate the loop through the array in reverse order.
STEP 3: The loop will start from for i in range(size).
STEP 4: Print the element arr[i] in each iteration.

Python Program for reverse an array or list

size=int(input("ENTER ARRAY SIZE: "))
arr=[]
print("ENTER THE ELEMENTS: ")
for i in range(size):
    element=int(input())
    arr.append(element)
print("THE REVERSED ARRAY IS: ",*arr[::-1],sep="\n") 
Output
ENTER ARRAY SIZE: 5
ENTER THE ELEMENTS:
12
91
23
43
9
THE REVERSED ARRAY IS:
9
43
23
92
12