Python Code for Reverse an Array
Reverse an Array using Python
Here, in this page we will discuss the program to reverse an array using python programming language. We will discuss different approaches to reverse the array in this page and compare the complexity of different approaches.
Different Approaches :
- Method 1 : Using Swapping
- Method 2 : Using Recursion
- Method 3 : Using Python List slicing
Method 1 :
- Take two variables say start = 0 and end= arr.len()-1
- Run a loop till start < end
- Swap arr[start] with arr[end]
- Increment start and decrement end by 1
- Print array
Example :
Let's array is arr[5] = [10, 20, 30, 40, 50] n = 5i = 0, j = 4
As (0 is less than 4) swap(arr[0],arr[4])
arr[0]=50 and arr[4]=10, i++(i.,e i=1) and j--(i.e, j=3)
Again (1 is less than 3) swap(arr[1],arr[3])
arr[1]=40 and arr[3]=20, i++(i.,e i=2) and j--(i.e, j=2)
Now, i is not less than j (as i=2 and j=2) so loop gets terminate and original array get reversed.
Method 1 : Code in Python
Run
def reverseList(A, start, end): while start < end: A[start], A[end] = A[end], A[start] start += 1 end -= 1 # Driver function to test above function A = [10, 20, 30, 40, 50] reverseList(A, 0, 4) print(A)
Output
[50, 40, 30, 20, 10]
Method 2 :
- Create a recursive function reverseList and pass array , start and end index of array.
- Recursively call the function reverseList.
Method 2 : Code in Python
Run
def reverseList(A, start, end): if start >= end: return A[start], A[end] = A[end], A[start] reverseList(A, start+1, end-1) # Driver function to test above function A = [10, 20, 30, 40, 50] reverseList(A, 0, 4) print(A)
Output
[50, 40, 30, 20, 10]
Method 3 :
In this method we reverse the array using list slicing
Method 3 : Code in Python
Run
def reverseList(A): print( A[::-1]) # Driver function to test above function A = [10, 20, 30, 40, 50] reverseList(A)
Output
[50, 40, 30, 20, 10]
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
def printOrder(arr, n):
arr.sort()
mid = n//2
s1 = arr[:mid]
s2 = arr[:mid:-1]
print(s1+s2)
# Driver code
arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ]
n = len(arr)
printOrder(arr, n)
Hey there, Thanks for commenting, kindly join our Discord server, our mentors will guide you further precisely will all your queries.🙌
#reversing the array
def rev(arr):
for i in range(len(arr)//2):
arr[i],arr[-i-1]=arr[-i-1],arr[i]
return arr
print(rev([2,3,4,5,5,6,0]))
The best solution, That I explored
a=[1,2,3,4,5]
c=[]
for i in range(1,len(a)+1):
c.append(a[-i])
print(c)
l=[1,2,3,8,3,3,0,98765,222,222]
a=[]
for i in range(len(l),0,-1):
c=l[i-1]
a.append(c)
print(a)
n = int(int(input()))
arr = []
sum = 0
for i in range(n):
ele = int(input())
arr.append(ele)
print(arr)
print(“the reverse”,arr[::-1])
a = input(“enter your sentence : “)
print(a[::-1])
#OUTPUT
enter your sentence : gnirts rof si siht
this is for string
a=list(map(int,input().split()))
print(a[::-1])
THIS LOOKS BETTER :
x = int(input(“Enter Total Elements : “))
y = []
for i in range(1, x+1):
z = input(“Enter Element {} : “.format(i))
y.append(z)
print(“Array is : {}”.format(y))
print(“Reversed Array is : {}”.format(y[::-1]))