Python List reverse() Method | Explained

List data structure is used to store a large collection of integer, string, float, etc. data. It takes the data inside the square bracket “[ ]” using the comma-separated syntax. Python supports various inbuilt methods to perform different operations on a list. For instance, sorting, cleaning, removing, adding, and reversing the list data structure. To reverse a list element, the “reverse()” method is used in Python.

This write-up will provide a detailed overview of the Python list “reverse()” method with appropriate examples. The contents discussed in this article are listed below:

So let’s get started!

What is the Python List reverse() Method?

The “reverse()” method of Python lists reverses the order of the element of the given list. It only updates the existing list by reversing its elements and doesn’t return any value. Because it can modify the existing list, the new memory will not be used for reversing the list. The syntax of the reverse() method is shown below:

list.reverse()

Let’s begin with our first Python list reverse() method example:

Example 1: Using reverse() Method to Reverse the Numeric List

In the below example, the “reverse()” method reverses the input list.

Code:

# create a list
list_num = [10, 20, 30, 40]

# reverse the list elements
list_num.reverse()
print('Reversed List:', list_num)

In the above code:

  • The list containing numeric values is initialized.
  • The “reverse()” method is used along with the dot operator to reverse the given list elements.

Output:

The above output verified that the list had been reversed.

Example 2: Using reverse() Method to Reverse the String List

In the example given below, the “reverse()” method reverses the string of the list.

Code:

# create a list
list_string = ['Python', 'Ubuntu', 'Linux']

# reverse the list elements
list_string.reverse()
print('Reversed List:', list_string)

In the above code:

  • The list containing string values is initialized in the program.
  • The “reverse()” function reverses the given list.

Output:

The above output shows that the list elements have been successfully reversed.

Example 3: Using reversed() Function to Reverse the Sequence of the List

In the example given below, the “reversed()” function is used to reverse the list. It retrieves a reversed, iterable object. Below is the syntax of the reversed function:

reversed(sequence)

The parameter “sequence” takes any iterable object like a list.

Let’s understand the working of the “reversed()” function by the following example:

Code:

# create a list
list_string = ['Python', 'Ubuntu', 'Linux']
reverse_list = reversed(list_string)
for i in reverse_list:
  print(i)

In the above code:

  • The “reversed()” function takes a list sequence variable as an argument and returns the iterable object in reversed order.
  • The “for” loop iterates over the reverse list and prints all the elements of a list sequence.

Output:

The above output shows that the string of the list has been reversed successfully.

That’s it from this guide!

Conclusion

To reverse the list element, the “reverse()” method is used in Python. The “reversed()” function also accepts the list as an argument and returns the reversed iterable object. The “reverse()” function does not accept any argument and also does not retrieve any value. This method only reversed the existing list by using the list variable and the dot operator, such as “list.reverse()”. This article presented a detailed guide on the Python list reverse() method with examples.