Extending a list in #Python

>>> a = []
>>> a.extend([2, 1]) # neat
>>> a += [3, 4] # hmm ok
>>> a += (7, 11) # huh?
>>> a[-1:] += [18, 29] # uh oh
>>> a[:] = [*a, 47, 76] # 😦
>>> a
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76]

#pythonoddity

@treyhunner Not all of them are extending a list. At least a couple (can't check right now, but definitely the last one) are creating a new list object.

@b11c believe it or not, all of them, the last one included, actually mutate the original list.

Even += mutates the list. For lists, += is pretty much syntactic sugar for extend.

@treyhunner Yep, you're right. The += is not surprising though, that's exactly what it's meant to do.