Reverse The Array Using Python - PrepInsta
Maybe your like
Unlock this article for Free, by logging in
Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Python Code for Reverse an ArrayOctober 7, 2022
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 = 5 i = 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] Related PagesFind Second Smallest Element in an Array
Calculate the sum of elements in an array
Sort first half in ascending order and second half in descending
Sort the elements of an array
Finding the frequency of elements in an array
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
Get Prime
Login/Signup to comment
12 comments on “Python Code for Reverse an Array”
-
Chirag
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)
Log in to Reply -
PrepInsta Support
Hey there, Thanks for commenting, kindly join our Discord server, our mentors will guide you further precisely will all your queries.?
Log in to Reply -
Manoj
arr= [10, 89, 9, 56, 4, 80, 8,2] arr.reverse() print(arr)
Log in to Reply -
Bharath
#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
Log in to Reply -
Rahul
a=[1,2,3,4,5] c=[] for i in range(1,len(a)+1): c.append(a[-i]) print(c)
Log in to Reply -
MANOJ
arr=[1,2,3,4,5] Print(arr[::-1])
Log in to Reply -
RADHIKA
arr = [10, 13, 17, 11, 34, 21] A=arr.reverse() print(arr)
Log in to Reply -
Anshul
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)
Log in to Reply -
Anirudh
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])
Log in to Reply -
44_Harsh
a = input(“enter your sentence : “) print(a[::-1]) #OUTPUT enter your sentence : gnirts rof si siht this is for string
Log in to Reply -
Ashish
a=list(map(int,input().split())) print(a[::-1])
Log in to Reply -
KIRA
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]))
Log in to Reply
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
Select your Passing out Year 2022 2023 2024 2025 2026 2027 2028 2029 Others Get Hiring UpdatesTag » How To Reverse An Array Python
-
Reverse An Array In Python - 10 Examples - AskPython
-
How Do I Reverse A List Or Loop Over It Backwards? - Stack Overflow
-
Reversing A List In Python - GeeksforGeeks
-
Python | Reverse A Numpy Array - GeeksforGeeks
-
How To Use Python To Reverse A List Or Array - MakeUseOf
-
Python Reverse List – Reversing An Array In Python - FreeCodeCamp
-
Python Reverse List – How To Reverse A Range Or Array
-
Python Program To Print The Elements Of An Array In Reverse Order
-
Python Reverse NumPy Array
-
5 Methods To Reverse Array In Python (reverse, Recursion Etc)
-
How To Reverse An Array In Python [Flip Array] - CodeItBro
-
NumPy: Reverse An Array - W3resource
-
Python List Reverse() - Programiz
-
Reverse An Array In Python Code Example - Code Grepper