Python

How to Reverse a list in Python?

How to Reverse a list in Python?

Introduction

In Python, a list can be defined as a dynamic array. In other words, it is a pack of elements, enclosed in square brackets ‘[]’, and the elements in it are separated by commas.

There are several methods with which we can operate on a list. One of them is the reverse() method, which we are going to discuss in this article.

Python List reverse() is an inbuilt list method with which we can reverse the elements of the list. By reverse, we mean to change the positions of elements. It does not use any extra space it just makes changes to the original list.

Syntax: name_of_list.reverse()
Parameters: No parameters are required.
Returns: It does not return anything, reverses the objects in the list.

The best way to understand the reverse() function in Python is to look at some examples

Examples of Python List reverse()

Code

list = [1,2,3,4,5]

list.reverse()

print(list)

Output

[5, 4, 3, 2, 1]

Code

list = ['a','b','c','d','e']

list.reverse()

print(list)

Output

['e', 'd', 'c', 'b', 'a']

Code

list = [['a','b','c','d','e'],[1,2,3,4,5],["Welcome","To","Board","Infinity"]]

list.reverse()

print(list)

list[0].reverse()

print(list)

list[1].reverse()

print(list)

list[2].reverse()

print(list)

Output

[['Welcome', 'To', 'Board', 'Infinity'], [1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e']]

[['Infinity', 'Board', 'To', 'Welcome'], [1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e']]

[['Infinity', 'Board', 'To', 'Welcome'], [5, 4, 3, 2, 1], ['a', 'b', 'c', 'd', 'e']]

[['Infinity', 'Board', 'To', 'Welcome'], [5, 4, 3, 2, 1], ['e', 'd', 'c', 'b', 'a']]

Error in reverse() method

When anything other than a list is used to reverse then it returns an attribute error.

Code

a = 10

a.reverse()

print(a)

Output

Traceback (most recent call last):

  File "main.py", line 2, in <module>

    a.reverse()

AttributeError: 'int' object has no attribute 'reverse'

Practical Applications

Code

l1 = [1,2,3,2,1]

l2 = l1.copy()

l2.reverse()

if(l2==l1):

    print("isPalindrome")

else:

    print("isNotPalindrome")

Output

isPalindrome

Code

l1 = [1,2,3,4,1]

l2 = l1.copy()

l2.reverse()

if(l2==l1):

    print("isPalindrome")

else:

    print("isNotPalindrome")

Output

isNotPalindrome

Examples

Code

tup = ('P', 'y', 't', 'h', 'o', 'n')

print(list(reversed(tup)))


r = range(5, 9)

print(list(reversed(r)))


l = [1, 2, 4, 3, 5]

print(list(reversed(l)))

Output

['n', 'o', 'h', 't', 'y', 'P']

[8, 7, 6, 5]

[5, 3, 4, 2, 1]

Code

class City:

    city = ['Delhi', 'Mumbai', 'Chandigarh', 'Kolkata', 'Pune']


    def __reversed__(self):

        return reversed(self.city)


c = City()

print(list(reversed(c)))


Output

['Pune', 'Kolkata', 'Chandigarh', 'Mumbai', 'Delhi']

Code

def r(s):

    s = s[::-1]

    return s

  

s = "Welcome User"

  

print("Original string: ", end="")

print(s)

  

print("Reversed string: ", end="")

print(r(s))

Output

Original string: Welcome User

Reversed string: resU emocleW

Code

def rev(s):

    s = "".join(reversed(s))

    return s

  

s = "Board Infinity"

  

print("Original string: ", end="")

print(s)

  

print("Reversed string: ", end="")

print(rev(s))


write your code here: Coding Playground

Output

Original string: Board Infinity

Reversed string: ytinifnI draoB