How to Reverse a Python List in-place and Understand Common Mistakes

Understand the difference

What does in-place mean? When reversing a list it means to not create a new list.

In-place reversing can be done by calling reverse().

a = [i for i in range(19)]
b = a
print("Before reverse")
print(a)
print(b)
a.reverse()
print("After reverse")
print(a)
print(b)

Will result in the following.

Before reverse
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
After reverse
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

While using the slicing to get the list reversed will create a copy.

a = [i for i in range(19)]
b = a
print("Before reverse")
print(a)
print(b)
a = a[::-1] # Reverse using slicing
print("After reverse")
print(a)
print(b)

Resulting in.

Before reverse
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
After reverse
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

It creates a new copy of the list a and keeps the original, which b points at, untouched.

Correct method: Swapping in a loop

Reversing a list in-place can be achieved with a call to reverse() as was shown above. A common question to get asked during a job-interview is to actually show an implementation of reversing.

As you might guess, you can achieve that by swapping elements.

[0, 1, 2, 3, 4, 5, 6]
# Swap first and last
[6, 1, 2, 3, 4, 5, 0]
# Swap second and second last
[6, 5, 2, 3, 4, 1, 0]
# Swap third and third last
[6, 5, 4, 3, 2, 1, 0]
# Swap the middle with, ah, itself (just kidding, this step is not needed)
[6, 5, 4, 3, 2, 1, 0]

How can you implement that? Let’s try.

a = [i for i in range(19)]
b = a
print(a)
print(b)
for i in range(len(a)//2):
    a[i], a[len(a) - i - 1] = a[len(a) - i - 1], a[i]
print(a)
print(b)

Resulting in the following expected output.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Again notice, that I make b point to a, in order to ensure and convince you that we make the reversing in-place and not of a copy. If the print(b) would not print the identical output as print(a), we would have a problem to explain.

Pitfall: List comprehension (incorrect)

But wait a minute? Doesn’t list comprehension mean making a new list based on an existing list?

Correct!

But we can actually circumvent that by using some syntax (or can we? Recommend you read it all).

a = [i for i in range(19)]
b = a
print(a)
print(b)
a[:] = [a[len(a) - i - 1] for i in range(len(a))]
print(a)
print(b)

The slice assignment a[:] enforces Python to do assign, or override, the original values of a. Even if the output shows the following.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

The catch is that [a[len(a) – i – 1] for i in range(len(a))] creates a new list, but the slice assignment ensures to override the values of a.

Bummer.

Conclusion

It is easy to make false conclusions like the example above shows. One of the beauties of Python is the extreme power on a high level. The price is that many things are hard to understand unless you dive deep into it.

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

Leave a Comment