Python List reverse Function

This Python function is used to reverse a given list, and its syntax is

list_name.reverse()

This function reverses the given list of items. The below code reverses the elements in an integer list.

rList = [10, 190, 120, 180, 120, 105]

rList.reverse()

print(rList)
[105, 120, 180, 120, 190, 10]

In this example, we declared a string list. Next, we used the reverse function on it.

TIP: Please refer to the List and methods articles to understand everything about them in Python.

FruitRevList = ['Orange', 'Banana', 'Kiwi', 'Watermelon', 'Grape', 'Blackberry']

FruitRevList.reverse()

print(FruitRevList)
['Blackberry', 'Grape', 'Watermelon', 'Kiwi', 'Banana', 'Orange']

Python List reverse Function Example

This example method reverses the list of items.

Fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry']
numbers = [9, 4, -5, 0, 22, -1, 2, 14]

print("Original : ", Fruits)
print("Original number : ", numbers)

Fruits.reverse()
print("\nReverse : ", Fruits)

numbers.reverse()
print("New Reverse Number : ", numbers)
Original :  ['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry']
Original number :  [9, 4, -5, 0, 22, -1, 2, 14]

Reverse :  ['Blackberry', 'Grape', 'Kiwi', 'Banana', 'Orange', 'Apple']
New Reverse Number :  [14, 2, -1, 22, 0, -5, 4, 9]

Let me use this method on Mixed Items.

MixedRevList = ['apple',  1, 5, 'Kiwi', 'Mango']

MixedRevList.reverse()

print(MixedRevList)
['Mango', 'Kiwi', 5, 1, 'apple']

This time, we used it on the Nested list.

MixedRevList = [[171, 222], [32, 13], [14, 15], [99, 76]]

MixedRevList.reverse()

print(MixedRevList)
[[99, 76], [14, 15], [32, 13], [171, 222]]

Python reverse user entered integer list items

This program is the same as the first example. However, we are allowing the user to enter the length of it. Next, we used For Loop to add numbers to it.

intRevList = []

number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intRevList.append(value)

print("Before is : ", intRevList)
intRevList.reverse()
print("After is  : ", intRevList)
Python Reverse List Function 5

This program allows users to enter their own string or words and then Reverse them

strRevList = []

number = int(input("Please enter the Total Number of Elements: "))
for i in range(1, number + 1):
    item = input("Please enter the Value of %d Element : " %i)
    strRevList.append(item)

print("Before. : ", strRevList)
strRevList.reverse()
print("After   : ", strRevList)
Please enter the Total Number of Elements: 4
Please enter the Value of 1 Element : Apple
Please enter the Value of 2 Element : Kiwi
Please enter the Value of 3 Element : Orange
Please enter the Value of 4 Element : Banana
Before :  ['Apple', 'Kiwi', 'Orange', 'Banana']
After  :  ['Banana', 'Orange', 'Kiwi', 'Apple']

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.