It would appear that in Python, list += x
works for any iterable x
:
In [6]: l = []
In [7]: l += [1]
In [8]: l += (2, 3)
In [9]: l += xrange(5)
In [10]: l
Out[10]: [1, 2, 3, 0, 1, 2, 3, 4]
Is this behaviour documented anywhere?
To contrast this with list + x
, the latter only works if x
is also a list
. This is spelled out in the documentation.
+=
operator mimicsextend
. I'll see if I can find something to confirm this. - RocketDonkey+=
and lists. Is there some part I am overlooking? - NPEThe
i' in__iadd__' stands for
in-place, and if you call the module
dis` on+=
then you'll see that it is in-place add only. - Ashwini Chaudhary__iadd__
documentation that " These methods should attempt to do the operation in-place (modifying self) [...]". - user395760