How to reverse a list in Python
Use slice syntax lst[::-1] for a new reversed list, or lst.reverse() to reverse in place. Both are O(n).
nums = [1, 2, 3, 4, 5]
reversed_nums = nums[::-1]
print(reversed_nums) # [5, 4, 3, 2, 1]The slice nums[::-1] takes every element from start to end with step -1, which walks the list backwards and returns a NEW list. The original list stays untouched. This is the most idiomatic Python approach when you want to keep both the original and the reversed version.
If you don't need the original, lst.reverse() modifies the list in place and returns None β saves the memory of a copy but discards the original order.
Variations
In-place reverse (mutates the original)
nums = [1, 2, 3, 4, 5]
nums.reverse()
print(nums) # [5, 4, 3, 2, 1]Faster + zero-copy, but the original order is lost.
reversed() iterator (for a for-loop)
for n in reversed([1, 2, 3, 4, 5]):
print(n)Returns a lazy iterator β cheapest option when you only need to walk once.
Reverse a string (same slice trick)
s = "hello"
print(s[::-1]) # 'olleh'Slicing works on any sequence, including strings and tuples.
Get one Python lesson + one career idea every Friday
No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.