>>> list1 = []
>>> list2 = list1
>>> list2 += [1]
>>> print list1
[1]
Comparing this to
>>> list1 = []
>>> list2 = list1
>>> list2 = list2 + [1]
>>> print list1
[]
Is there a reason why the '+='-operation modifies the original list?
EDIT: just to make my question a bit clearer
In most of the languages that I know the '+='-operator doesn't work this way and I would like to know why it was designed in this way in python.
some examples:
Ruby
irb(main):001:0> l = []
irb(main):002:0> a = l
irb(main):003:0> a += [2]
irb(main):004:0> l
=> []
Scala etc..
An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is x += 1 which is expanded to x = x + (1) .
The operators + and * can be used to concatenate lists and multiply lists. The compound operators += and *= can concatenate lists and multiply lists and pass the new identity to the original list.
The means that += , -= , *= , /= , //= , %= , **= , >>= , <<= , &= , ^= , and |= are all assignment operators. We can see obvious parallels between sums using += , and products using *= . In the case of mutable objects, this augmented assignment can take on special ...
Assignment Operator is nothing but the "equal to - =" operator. This operator assigns a value to a variable. Augmented Assignment Operators performs operations and assigns the value to the same variable.
Lists in Python as stored by reference.
This means that when you do list2 = list1
, you are not making a copy of the list - you are merely saying "list2
refers to the same thing list1
does," namely, the list you originally created when you did list1 = []
.
Python specifies +=
to mean "append in place" for lists, because most of the time when you're using +=
on lists, that's what you want to do - you usually don't want to create new lists every single time you add an element.
Thus, when you append to list2
, which "refers to the same object list1
does," and then read from list1
, you see the appended item, as is expected since both of them point at the same list.
With +
, however, a new list is always created because it doesn't make sense to modify either of the operands in place (since a+b
doesn't imply the modification of a
or b
).
Therefore, when you do list2 = list2 + [1]
, you create a new list that has all of the contents of the original object pointed to by list2
and also 1
, and then say that list2
now references that new list. Since it now references a different list than list1
does, when you go to read from list1
you still see the original list without the extra 1
.
From the Python 2.6.4 documentation, Section 6.2.1. (Augmented assignment statements)
An augmented assignment expression like
x += 1
can be rewritten asx = 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.
[Emphasis added]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With