Print List in reverse order in Python

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.

Advertisements

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.

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.

Advertisements

Thanks for reading.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top