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.

Easy Python 3.9+ Aug 9, 2026 Lists & loops 14 views 0 copies

Python code

14 lines
Python 3.9+
def 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

stdout
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

  1. Use `lst.reverse()` if you're allowed to use the built-in method — it also works in place
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Lists & loops

Related tutorials and quizzes for this topic.