nagy wrote: > Thanks, Kirk. > I considered the += as only a shorthand notation for the assignment > operator. > Since for lists + is simply a concatetation, I am not sure it x=x+[2] > is creating a brand > new list. Could you refer me to any documentation on this?
Yes: http://docs.python.org/ref/augassign.html "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." This behavior is only logical. Consider: >>> x = [2] >>> y = x + [4] After these operations, we have two lists: x (the list [2]) and y (the list [2, 4]). This is because the expression "x + [4]" creates a new list. We then bind this new list to the name 'y', and leave the name 'x' alone. If we then say this: >>> x = x + [6] We are doing much the same operation. We are creating a new list (the list [2, 6]), and binding it to the name 'x'. The list [2], previously bound to 'x', is no longer bound to anything, so Python frees it. The augmented assignment, as I went over previously, attempts to modify the list object directly. Any names bound to the object (or any other objects that reference the object) will see the changes. -Kirk McDonald -- http://mail.python.org/mailman/listinfo/python-list