Add vs in-place add of str to list

rs387 rstarkov at gmail.com
Thu Oct 2 08:57:24 CEST 2008


Hi

I'm trying to understand why it is that I can do

>>> a = []
>>> a += 'stuff'
>>> a
['s', 't', 'u', 'f', 'f']

but not

>>> a = []
>>> a = a + 'stuff'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list

Can someone explain the logic? Why is the in-place add not a type
error but the usual add is? (This applies to both Python 2.6rc1 and
3.0b2)

Thanks
Roman



More information about the Python-list mailing list