Concatenating lists

Concatenating lists using the addition operator results in a new list without modification of either of the operands:

>>> m = [2, 1, 3]
>>> n = [4, 7, 11]
>>> k = m + n
>>> k
[2, 1, 3, 4, 7, 11]

Whereas the augmented assignment operator += modifies the assignee in place:

>>> k += [18, 29, 47]
>>> k
[2, 1, 3, 4, 7, 11, 18, 29, 47]

A similar effect can also be achieved using the extend() method:

>>> k.extend([76, 129, 199])
>>> k
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199]

Augmented assignment and the extend() method will work with any iterable series on the right-hand-side.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset