How to Reverse a List in Place Without Using reverse() in Python
A two-pointer while loop swaps elements from both ends toward the center to reverse a list in place without creating a copy.
Python code
14 linesdef reverse_list_in_place(lst):
left = 0
right = len(lst) - 1
while left < right:
lst[left], lst[right] = lst[right], lst[left]
left += 1
right -= 1
if __name__ == "__main__":
my_list = [1, 2, 3, 4, 5]
print("Original:", my_list)
reverse_list_in_place(my_list)
print("Reversed:", my_list)
Output
Original: [1, 2, 3, 4, 5]
Reversed: [5, 4, 3, 2, 1]
How it works
The function uses two indices: left starts at 0 and right at len(lst) - 1. The while loop continues as long as left < right, swapping the elements at those positions using Python's tuple unpacking assignment (lst[left], lst[right] = lst[right], lst[left]). After each swap, left increments and right decrements, moving the pointers toward the center. When they meet or pass each other, the loop stops, and the list is fully reversed. This approach modifies the original list directly (in place) and requires no extra memory beyond the two pointer variables — a simple O(n/2) operation.
Common mistakes
- Forgetting that list slicing like `lst[::-1]` creates a new list instead of modifying the original one
- Using a loop that swaps every pair twice, resulting in no net change
- Not handling an empty list or a list with one element — the loop just doesn't run, which is correct
Variations
- Use `lst.reverse()` if you're allowed to use the built-in method — it also works in place
- Use slicing `reversed_lst = lst[::-1]` when you need a new list and the original can stay unchanged
Real-world use cases
- Reversing a deck of cards in a game engine without allocating extra memory, for performance-sensitive loops.
- Flipping the order of a queue's pending jobs in memory before processing them again.
- Implementing a custom in-place reverse when working with large lists where a copy would exceed memory limits.
Sponsored
More from Lists & loops
- Check if List is Sorted Ascending in Python easy
- Compare Two Lists in Python: Common, Only in First, Only in Second easy
- Convert a List of Integers to a Comma-Separated String in Python easy
- Enumerate a Python List with a Custom Start Index easy
- Extract Data by Type from a List in Python: Numbers and Strings easy
- Find All Occurrences of an Item in a Python List easy
Keep learning
Related tutorials and quizzes for this topic.