Python Code for Reverse an Array

Reverse an array using python

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
Reverse the array in python

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

9 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)


  • 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


  • 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)


  • 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])


  • 44_Harsh

    a = input(“enter your sentence : “)
    print(a[::-1])
    #OUTPUT
    enter your sentence : gnirts rof si siht
    this is for string


  • 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]))