Menu Close

Reverse a List in Python

Reverse a list in Python

In this article, you will learn about how to reverse a list in python. Here we will see different-different ways to write a python program to reverse a list.

In this python program, we will use the reversed() function, slicing, and custom function to reverse list items.

To understand how to reverse a list in python you should have basic knowledge of the function and for loop in python.

Before going through this article, we will see little about python list.

What is a list in Python?

The list is a collection of data type in python that means the list is a collection of elements of different data type elements which is ordered, changeable and indexed or you can say the list is mutable.

Reverse a list using user-defined function

In this example we perform reverse of list using reversed() built-in function.

#user defined function to reverse a list
def reverse(lst):
	return [i for i in reversed(lst)]
    
# list 
mylst = [12, 23, 10, 14, 16, 15]

print("Orignal list:- ", mylst)
#call the function
print("Reversed list:- ", reverse(mylst))

Output

Orignal list:-  [12, 23, 10, 14, 16, 15]
Reversed list:-  [15, 16, 14, 10, 23, 12]

Reverse a list in Python using reverse() function

In this example we will write python program to reverse a list using list reverse() method.

def reverse(lst):
	lst.reverse()
	return lst
	
# list 
mylst = [1, 2, 3, 4, 5, 6]

print("Orignal list:- ", mylst)
#call the function
print("Reversed list:- ", reverse(mylst))

Output

Orignal list:-  [1, 2, 3, 4, 5, 6]
Reversed list:-  [6, 5, 4, 3, 2, 1]

Reverse a list in python using the slice technique

In this example, we will write a python program to reverse a list using the slice technique.

#user defined function
def reverse(lst):
	new_list = lst[::-1]
	return new_list
	

# list 
mylst = ['Windows', 'macOS', 'Linux', 'Ubuntu', 'Debian']

print("Orignal list:- ", mylst)
#call the function
print("Reversed list:- ", reverse(mylst))

Output

Orignal list:-  ['Windows', 'macOS', 'Linux', 'Ubuntu', 'Debian']
Reversed list:-  ['Debian', 'Ubuntu', 'Linux', 'macOS', 'Windows']

Example

Here we will ask a number from the user one by one for a specific number of times and then store all the numbers into a list and then reverse them.

num = int(input('Enter how many element you want in list:- '))
list1 = []

for i in range(1, num + 1):
    element = input(f'Enter {i} element:- ')
    list1.append(int(element))

print('List is:- ', list1)  
print('Reversed list is:- ', list1[::-1])

Output

Enter how many element you want in list:- 4
Enter 1 element:- 1
Enter 2 element:- 2
Enter 3 element:- 3
Enter 4 element:- 4
Original List is:-  [1, 2, 3, 4]
Reversed list is:-  [4, 3, 2, 1]

Conclusion

So that in this tutorial, we have seen how to reverse a list in python using different-different ways.
To reverse a list in python, we have used for loop, reversed() built-in function as well as a custom function. If you are a beginner at the python program, then this python example will very helpful for you.

If you like this article you can keep it continue for further python amazing examples.

Python programs

For more information:- Click Here

Python Program to Find Smallest Number in a List
Reverse a String in Python ( 5 best ways )

Was this post helpful?

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *