Table of Contents
In this tutorial, we will learn about the Python List reverse() method with the help of examples.
The reverse()
method reverses the elements of the list.
Example
# create a list of prime numbers prime_numbers = [2, 3, 5, 7] # reverse the order of list elements prime_numbers.reverse() print('Reversed List:', prime_numbers) # Output: Reversed List: [7, 5, 3, 2]
1. Syntax of List reverse()
The syntax of the reverse()
method is:
list.reverse()
2. reverse() parameter
The reverse()
method doesn’t take any arguments.
3. Return Value from reverse()
The reverse()
method doesn’t return any value. It updates the existing list.
4. Example 1: Reverse a List
# Operating System List systems = ['Windows', 'macOS', 'Linux'] print('Original List:', systems) # List Reverse systems.reverse() # updated list print('Updated List:', systems)
Output
Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']
There are other several ways to reverse a list.
5. Example 2: Reverse a List Using Slicing Operator
# Operating System List systems = ['Windows', 'macOS', 'Linux'] print('Original List:', systems) # Reversing a list # Syntax: reversed_list = systems[start:stop:step] reversed_list = systems[::-1] # updated list print('Updated List:', reversed_list)
Output
Original List: ['Windows', 'macOS', 'Linux'] Updated List: ['Linux', 'macOS', 'Windows']
6. Example 3: Accessing Elements in Reversed Order
If you need to access individual elements of a list in the reverse order, it’s better to use the reversed()
function.
# Operating System List systems = ['Windows', 'macOS', 'Linux'] # Printing Elements in Reversed Order for o in reversed(systems): print(o)
Output
Linux macOS Windows
Related posts:
Python Program to Get the Class Name of an Instance
Python Program to Compute the Power of a Number
Python Program to Count the Occurrence of an Item in a List
Python Dictionary values()
Python Program to Check Armstrong Number
Python Program to Find HCF or GCD
Python globals()
Python Program to Find Hash of File
Python Set symmetric_difference_update()
Python Set intersection()
Python List sort()
Python Program to Find the Factors of a Number
Python Dictionary keys()
Converting between an Array and a List in Java
Copy a List to Another List in Java
Python Program to Make a Flattened List from Nested List
Python Program to Print Colored Text to the Terminal
Python Program to Create Pyramid Patterns
Python Set remove()
Python Program to Check if a Number is Odd or Even
Removing all duplicates from a List in Java
Python Program to Find Numbers Divisible by Another Number
Python Tuple index()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python compile()
Python String rjust()
Python Program to Sort a Dictionary by Value
Python abs()
Python Directory and Files Management
Python Numbers, Type Conversion and Mathematics
Python Program to Find All File with .txt Extension Present Inside a Directory