In Python, there are three main approaches to reversing the order of a list.
These methods differ in behavior: some create a new list, others modify the original list, and one handles data as an iterator. It is important to choose the right method for your specific use case.
Here, I will explain each implementation using “web browser history” data as an example.
1. Using Slicing [::-1]
This is the most concise and “Pythonic” way to write it. It does not modify the original list but creates a new copy in reverse order.
# Browsing history (assuming stored from oldest to newest)
browsing_history = ["Top Page", "Search Results", "Article A", "Article B"]
# Specify -1 for the step in slice syntax [start:stop:step]
# This generates a new list retrieving elements from the end to the start
recent_history = browsing_history[::-1]
print(f"Original: {browsing_history}")
print(f"Reversed: {recent_history}")
Execution Result
Original: ['Top Page', 'Search Results', 'Article A', 'Article B']
Reversed: ['Article B', 'Article A', 'Search Results', 'Top Page']
2. Using the Built-in Function reversed()
The reversed() function returns an iterator for accessing elements in reverse order.
This is effective when handling huge lists if you do not want to generate a new reversed list in memory immediately. If you need it as an actual list object, convert it using list().
browsing_history = ["Top Page", "Search Results", "Article A", "Article B"]
# reversed() returns an iterator, so use it in a for-loop or convert with list()
reverse_iterator = reversed(browsing_history)
# Convert to a list to materialize the data
recent_history_v2 = list(reverse_iterator)
print(f"Reversed by function: {recent_history_v2}")
3. Using the List Method reverse()
The reverse() method, which belongs to the list object itself, rewrites the original list in reverse order (destructive change).
It is memory efficient because it does not create a new list, but the original order is lost. Note that the return value is None.
browsing_history = ["Top Page", "Search Results", "Article A", "Article B"]
# Reverse the list itself (there is no return value)
browsing_history.reverse()
print(f"Destructively Reversed: {browsing_history}")
Summary
| Method | Syntax | Features | Original List |
| Slicing | l[::-1] | Most concise. Creates a new list. | Unchanged |
| Function | reversed(l) | Returns an iterator. Memory efficient. | Unchanged |
| Method | l.reverse() | Rewrites the list itself. | Modified |
Google スプレッドシートにエクスポート
Basically, use slicing [::-1]. Consider the other methods if memory efficiency is a priority or if you do not need to keep the original list.
