Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    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.

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment