Python List reverse() method [With Examples]

In this Python article, we will take a deep dive into the Python List reverse() method – a powerful and handy technique for reversing the elements of a Python list in place.

We will cover its syntax, usage, and explore some examples to better understand its practical applications.

List reverse() method in Python

Below are the topics that we are doing to discuss in this article:

  • Introduction to Python List reverse() method
  • Syntax of the reverse() method
  • Purpose and use cases of the reverse() method

Python List reverse() method

The reverse() method is a built-in Python method for lists, which is used to reverse the order of elements in a list object. This in-place reversal method modifies the original Python list and does not create a new one, making it memory-efficient.

It is essential to note that this method works only with Python list objects and cannot be used with other iterables such as strings or tuples.

The syntax of the list reverse() method is as follows:

list.reverse()

Here, ‘list’ is the Python list object that we want to reverse. The method does not take any arguments, and it does not return any value, as it reverses the list in place.

reverse() method in Python List Examples

Let’s explore some examples to understand how the Python list reverse() method works.

Example#1 Reversing a list of integers

population = [327, 328, 329, 330, 331]
population.reverse()
print(population)

This example demonstrates a Python list of integers representing the US population in millions from 2018 to 2022. The list is reversed, which displays the population from the most recent year to the oldest.

READ:  How to Initialize Dictionary Python with 0

Output:

Python List reverse method
Python List reverse method

Example#2 Reversing a list of strings

states = ["California", "Texas", "Florida", "New York"]
states.reverse()
print(states)

In this example, a Python list of strings representing some of the most populous states in the US is reversed. After applying the reverse() method, the list displays the states in the opposite order.

Output:

Python List reverse method example
Python List reverse method example

Example#3 Reversing a list of mixed data types

us_data = [331, "United States", 9.834, True]
us_data.reverse()
print(us_data)

Here, a Python list of mixed data types representing US data is reversed. The list contains the population in millions (331), the country name (“United States”), the land area in million square kilometers (9.834), and a boolean value indicating whether it’s a member of the G7 (True).

The reverse() method is applied to the us_data list, which reverses the order of the elements in the list.

Output:

List reverse method in Python
List reverse method in Python

Example#4 Reversing a list of elements for algorithms that require processing data in reverse order

avg_temps = [32, 35, 45, 55, 65, 75, 80, 78, 70, 58, 48, 38]
avg_temps.reverse()
print(avg_temps)

In this case, we reverse a Python list of average monthly temperatures in Fahrenheit for a US city to process or analyze them in reverse chronological order, i.e., starting from the most recent month and going back in time.

Output:

List reverse method in Python example
List reverse method in Python example

Example#5 Sorting a list in descending order by first sorting it in ascending order and then reversing it

city_population = [("New York", 8550405), ("Los Angeles", 3971883), ("Chicago", 2720546), ("Houston", 2296224), ("Phoenix", 1660272)]

city_population.sort(key=lambda x: x[1])
city_population.reverse()
print(city_population)

We first sort a Python list of tuples containing US city names and their populations in ascending order by population. Then, we reverse the sorted Python list to display the cities in descending order of population, i.e., from the most populous city to the least.

READ:  Count function in Python string

Output:

reverse method in Python List Examples
reverse method in Python List Examples

Example#6 Reversing the elements of a list as a part of data preprocessing

stock_prices = [120.45, 121.32, 123.67, 125.21, 124.78, 123.45, 122.89]
stock_prices.reverse()
print(stock_prices)

We reverse a Python list of daily closing stock prices for a US company to analyze them in reverse chronological order. This could be useful for various financial analysis tasks, such as identifying trends or patterns in recent stock prices.

Output:

reverse method in Python List
reverse method in Python List

Conclusion

Python list reverse() method is a powerful and convenient tool for reversing the order of elements in a list. Its in-place reversal approach saves memory, and its simplicity makes it easy to implement in various scenarios.

You may also like to read the following articles: