Search code examples
pythonlistequivalence

Question about reversed lists in Python


I am very new to python, as you will be able to tell.

If I have a list:

a = [1,2,3,2,1]

This evaluates to true:

a == a[::-1]

...but this evaluates to false:

a == a.reverse()

Why is that the case?


Solution

  • because .reverse() reverses the list in-place and returns none:

    >>> print a.reverse()
    None
    

    and a == None evaluates to False.