Python extend with an empty list bug?

Doug T. picture Doug T. · Feb 14, 2009 · Viewed 23.9k times · Source

Why does python 2.5.2 have the following behavior

>>>[2].extend([]) == [2]
False

>>> [2].extend([]) == None
True

$ python --version
Python 2.5.2

I assume I'm not understanding something here, but intuitively I'd think that [2].extend([]) should yield [2]

Answer

Rafał Dowgird picture Rafał Dowgird · Feb 14, 2009

Extend is a method of list, which modifies it but doesn't return self (returning None instead). If you need the modified value as the expression value, use +, as in [2]+[].