Python Program to Reverse List

Write a Python Program to reverse List Items with a practical example.

Python Program to Reverse List Items

This python program allows user to enter the length of a List. Next, we used Python For Loop to add numbers to the list.

# Python Program to Reverse List Elements

NumList = []

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))
    NumList.append(value)

NumList.reverse()
print("\nThe Result of a Reverse List =  ", NumList)
Python Program to Reverse List Elements 1

TIP: Python reverse function is used to reverse the elements in a List.

Program to Reverse List Items without using reverse

In this python program, we are using a While loop. Inside the while loop, we performed the Swapping with the help of the third variable. I suggest you refer Swap two Numbers article to understand the Python logic.

# Python Program to Reverse List Elements

NumList = []

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))
    NumList.append(value)

j = Number - 1
i = 0

while(i < j):
    temp = NumList[i]
    NumList[i] = NumList[j]
    NumList[j] = temp
    i = i + 1
    j = j - 1
    
print("\nThe Result of a Reverse List =  ", NumList)

Python reverse a list using a while loop output

Please enter the Total Number of List Elements: 3
Please enter the Value of 1 Element : 1
Please enter the Value of 2 Element : 2
Please enter the Value of 3 Element : 3

The Result of a Reverse List =   [3, 2, 1]

Program to Reverse List Items using Functions

This program to reverse List items is the same as the above. However, we separated the logic using Functions

# Python Program to Reverse List Elements

def reverse_list(NumList, num):
    j = Number - 1
    i = 0
    while(i < j):
        temp = NumList[i]
        NumList[i] = NumList[j]
        NumList[j] = temp
        i = i + 1
        j = j - 1
    
NumList = []
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))
    NumList.append(value)
    
reverse_list(NumList, Number)
print("\nThe Result of a Reverse List =  ", NumList)

Python reverse a list using functions output

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 10
Please enter the Value of 2 Element : 20
Please enter the Value of 3 Element : 30
Please enter the Value of 4 Element : 40
Please enter the Value of 5 Element : 50

The Result of a Reverse List =   [50, 40, 30, 20, 10]

Program to Reverse Items in a List using Recursion

This program reverses the List items by calling functions recursively

# Python Program to Reverse List Elements

def reverse_list(NumList, i, j):
    if(i < j):
        temp = NumList[i]
        NumList[i] = NumList[j]
        NumList[j] = temp
        reverse_list(NumList, i + 1, j-1)
    
NumList = []
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))
    NumList.append(value)
    
reverse_list(NumList, 0, Number - 1)
print("\nThe Result of a Reverse List =  ", NumList)

Output of a Python list reverse recursive function

Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : 13
Please enter the Value of 3 Element : 14
Please enter the Value of 4 Element : 15
Please enter the Value of 5 Element : 16
Please enter the Value of 6 Element : 27

The Result of a Reverse List =   [27, 16, 15, 14, 13, 12]