🕯️ Magic Note
The reverse() method modifies the list in place. It returns None, so you should not assign its result to a variable. This is different from slicing [::-1], which creates a new reversed list while leaving the original unchanged. reverse() is memory efficient because it rearranges the existing list without copying. The reversal happens in O(n) time, swapping elements from the ends toward the middle.
- Modifies the list in place, returns None
- Use reversed(mirror) to get an iterator without changing the original
- Use mirror[::-1] to create a new reversed list
- Works on lists only, not tuples or strings (they are immutable)
| Method | Returns | Original Affected | Memory |
|---|---|---|---|
| list.reverse() | None | Yes (in place) | No extra list |
| reversed(list) | Iterator | No | Small overhead |
| list[::-1] | New list | No | Full copy |
Python
# Reversing a list in place
mirror = [1, 2, 3, 4, 5]
print(f”Before: {mirror}”)
mirror.reverse()
print(f”After: {mirror}”)
# Output: Before: [1, 2, 3, 4, 5]
# Output: After: [5, 4, 3, 2, 1]
Python
# Common mistake: assigning the result
words = [“truth”, “mirror”, “reflection”]
result = words.reverse()
print(f”Original list: {words}”)
print(f”Value returned: {result}”)
# Output: Original list: [‘reflection’, ‘mirror’, ‘truth’]
# Output: Value returned: None
# The list is reversed, but result is None
Python
# Reverse vs slice vs reversed
original = [“a”, “b”, “c”, “d”]
# In place reverse
copy_for_reverse = original.copy()
copy_for_reverse.reverse()
print(f”reverse(): {copy_for_reverse}”)
# Slicing creates new list
sliced = original[::-1]
print(f”Slicing: {sliced}”)
# reversed() returns iterator
rev_iter = reversed(original)
print(f”reversed(): {list(rev_iter)}”)
# Output: reverse(): [‘d’, ‘c’, ‘b’, ‘a’]
# Output: Slicing: [‘d’, ‘c’, ‘b’, ‘a’]
# Output: reversed(): [‘d’, ‘c’, ‘b’, ‘a’]
# Original still [‘a’,’b’,’c’,’d’]
- Assigning list.reverse() to a variable, expecting the reversed list but receiving None
- Trying to reverse a tuple or string with .reverse(), causing an AttributeError
- Confusing reverse() with reversed(), using one when you need the other
⚡ Whisper
The mirror did not lie. It showed the truth from the other side. The reflection stayed. Only the order shifted. Some truths reveal themselves backwards. You look again and see what was always there, just turned around.