Python List reverse()

The reverse() method reverses the elements of a given list.

The syntax of reverse() method is:

list.reverse()

reverse() parameter

The reverse() function doesn't take any argument.


Return Value from reverse()

The reverse() function doesn't return any value. It only reverses the elements and updates the list.


Example 1: Reverse a List

# Operating System List
os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)

# List Reverse
os.reverse()

# updated list
print('Updated List:', os)

When you run the program, the output will be:

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

There are other several ways to reverse a list.


Example 2: Reverse a List Using Slicing Operator

# Operating System List
os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)

# Reversing a list	
#Syntax: reversed_list = os[start:stop:step] 
reversed_list = os[::-1]

# updated list
print('Updated List:', reversed_list)

When you run the program, the output will be:

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

Example 3: Accessing Individual Elements in Reversed Order

If you need to access individual elements of a list in reverse order, it's better to use reversed() method.

# Operating System List
os = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order
for o in reversed(os):
    print(o)

When you run the program, the output will be:

Linux
macOS
Windows