list.reverse()
Reverses a list in place (mutates) and returns None.
my_list.reverse()This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Reverses a list in place (mutates) and returns None.
The list.reverse() method performs an in-place reversal of the elements within a Python list. This operation is highly efficient with a time complexity of O(n) and a space complexity of O(1), as it does not require allocating additional memory for a new list structure. Unlike the sorted() or reversed() functions which return new objects, reverse() directly modifies the underlying array and returns None. This behavior follows Python's design philosophy of distinguishing between methods that mutate a collection and functions that return a new one. In multi-threaded environments, care must be taken as this operation is not atomic. If a copy is needed while reversing, the slicing syntax [::-1] is generally preferred for its readability and performance. The method handles empty lists and single-element lists gracefully without raising exceptions.
Quick reference
Syntax
my_list.reverse()
See it in practice
Examples
Basic List Reversal
languages = ['Python', 'Java', 'C++', 'Ruby']
languages.reverse()
print(languages)['Ruby', 'C++', 'Java', 'Python']
Demonstrates the standard usage of the reverse method to flip the order of strings in a list.
Verifying In-Place Modification
data = [10, 20, 30]
result = data.reverse()
print(f'List after reverse: {data}')
print(f'Method return value: {result}')List after reverse: [30, 20, 10] Method return value: None
Shows that the method modifies the original list and returns None, which is important for avoiding logic errors.
Reversing Mixed Data Types
mixed_list = [1, 'two', 3.0, [4]]
mixed_list.reverse()
print(mixed_list)[[4], 3.0, 'two', 1]
Confirms that the reverse method works on lists containing various objects and nested structures.
Debug faster
Common Errors
LogicError
Cause: Attempting to assign the result of the reverse() method to a variable, expecting it to contain the reversed list.
Fix: Call the method on the list and then use the list variable directly, or use the slicing syntax [::-1] if a returned value is required.
my_list = [1, 2, 3]
new_list = my_list.reverse() # new_list is now NoneAttributeError
Cause: Trying to call .reverse() on an immutable sequence like a string or a tuple.
Fix: Convert the sequence to a list using the list() constructor before reversing, or use slicing.
my_tuple = (1, 2, 3)
my_tuple.reverse() # Raises AttributeError: 'tuple' object has no attribute 'reverse'Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Reverses a list in place (mutates) and returns None.
LogicError: Call the method on the list and then use the list variable directly, or use the slicing syntax [::-1] if a returned value is required. AttributeError: Convert the sequence to a list using the list() constructor before reversing, or use slicing.