Adding (concatenating) tuples to lists in Python

22 Views Asked by At

In Python3, let me define simple a list and tuple as follows:

In [1]: alist = [1, 2, 3]
In [2]: atuple = (4, 5, 6)

If I attempt an "add" (+) of the tuple to the list and assign that to the original list I get a TyperError:

In [3]: alist = alist + atuple
TypeError: can only concatenate list (not "tuple") to list`

However, if I use the augmented assignment operator, it will append atuple to alist, it will work as intended:

In [4]: alist += atuple
In [5]: alist
Out[5]: [1, 2, 3, 4, 5, 6]

So, logically, why does the augmented assignment operator work, In [4], while the long form, In [3], throws are Error?

I want to understand why the augmented assignment operation allows appending a tuple to a list while the + operator does not.

0

There are 0 best solutions below