In this article, we will discuss different ways to print a list in reverse order in Python.
Table Of Contents
Method 1: Using reversed() function
In Python, we have a function reversed(), that accepts a sequence as argument, and returns a reverse iterator of values in the given sequence. We can pass our list object to the reversed() function, and print returned values. Basically, it will print the contents of list in reverse order. Let’s see an example,
listOfNum = [21, 22, 23, 24, 25, 26, 27] # print the list in reverse order print(list(reversed(listOfNum)))
Output:
[27, 26, 25, 24, 23, 22, 21]
It printed the list contents in reverse order.
Frequently Asked:
Method 2: Using list.reverse() function
List class has a function reverse(). It will reverse the order of elements in list in place. We can call this to reverse the contents of list, and then print the list. Let’s see an example,
listOfNum = [21, 22, 23, 24, 25, 26, 27] # Reverse the contents in list listOfNum.reverse() print(listOfNum)
Output:
[27, 26, 25, 24, 23, 22, 21]
It printed the list contents in reverse order.
Method 3: Using Slicing
We can slice the list from end to start in reverse order using slicing i.e.
Latest Python - Video Tutorial
list[ : : -1]
We need to pass the step size as -1, and leave the start
, and end
parameters to default values. Basically, it will give a new list with the reversed contents. Then we can print that new list. Let’s see an example,
listOfNum = [21, 22, 23, 24, 25, 26, 27] # print the list in reverse order print(listOfNum[::-1])
Output:
[27, 26, 25, 24, 23, 22, 21]
It printed the list contents in reverse order.
Summary
We learned about three different ways to print a list in reversed order.
Latest Video Tutorials