Python Reversing a List

Reversing a List in Python

The reverse() method in the list class reverses the order of items in a list. The item at the last index is moved to index 0, and the item originally at index 0 is moved to the last index.

Syntax

list.reverse()

Return Value

This method does not return any value because the list is reversed in place.

Example

The following example demonstrates how to reverse the order of a list.

lst = [25, 12, 10, -21, 10, 100]
print ("lst:", lst)
lst.reverse()
print ("Reversed list:", lst)

This will produce the following output −

lst: [25, 12, 10, -21, 10, 100]
Reversed list: [100, 10, -21, 10, 12, 25]

Leave a Reply

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