Home python Assignment operator + = for lists (append in place)

Assignment operator + = for lists (append in place)




How does the + = operator and similar ones work? That is, it is clear that x + = y is analogous to x = x + y .

But I ran into this problem:

x = []
y = (1,2,3)
x = x + y

In this case, there will be an error:

TypeError: can only concatenate list (not “tuple”) to list

But if you write

x + = y

That x value will be output as

[1, 2, 3]

Answer 1, authority 100%

x + = y for lists is equivalent to x.extend (y) and allows an arbitrary iterable object in place of y , for example:

& gt; & gt; & gt; import random
& gt; & gt; & gt; L = [1]
& gt; & gt; & gt; L + = (i for i in range (10) if random.random () & lt; .9)
& gt; & gt; & gt; L
[1, 0, 1, 3, 4, 6, 8, 9]

The result in this case may even change from run to run.

So x + = y works if x is a list even though y is a tuple (a tuple is iterable object).

If you swap x , y , the expression stops working: immutable objects such as tuples do not override __iadd __ is a method that implements the + = operator, and therefore tup + = [1] is equivalent to tup = tup + [1] which leads to the error shown in the question.

Adding lists and tuples is prohibited, since it is not clear what the result should be (list or tuple).

Details can be read by executing help ('+ = ') in the Python console or pydoc "+ =" from the command line :

An augmented assignment expression like “x + = 1” can be rewritten as
“x = x + 1” to achieve a similar, but not exactly equal effect. In the
augmented version, “x” is only evaluated once. Also, when possible,
the actual operation is performed in-place , meaning that rather than
creating a new object and assigning that to the target, the old object
is modified instead.

That is, x + = 1 and x = x + 1 are similar, but the results may differ as in this case.

Answer 2, authority 43%

For mutable collections in Python, the + = method works in such a way that it modifies the collection in place, as opposed to the + operator, which will create a new object.

So the code

x = [] # id (x) == 140412468236104
x + = (1, 2, 3) # id (x) == 140412468236104

roughly equivalent

x = [] # id (x) == 140412465906440
x.extend ((1, 2, 3)) # id (x) == 140412465906440

Using the + operator creates a new object:

x = [] # id (x) == 140412468319432
x = x + [1, 2, 3] # id (x) == 140412511200328

But remember that Python is a strongly typed language, so it is not permissible to use the + operator on a list with a tuple. This is why the TypeError error is raised. The + operator works only with objects of the same type (standard objects).

In your objects, you can always overload operators by adding whatever behavior you want.

Answer 3

How + = behaves is determined by the __iadd __ (self, value, /) object method.
Since the list class is implemented in C, dynamic typing does not work there the same as in Python, but the logic is implemented in accordance with the standard.
Anything that does not fall under the standard should cause exceptional behavior. The content type is checked before being processed and therefore an exception is thrown.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions