in python what's the relation between the loop variable and the elements in list when we do for loop?
for-loop, python
Solution
The relation is the same as with all variable binding in Python. The name is bound to the value. The differences you are seeing are because you are doing different things, sometimes, operating on the value and sometimes on the name.
For lists, `+=` extends the list in-place --- that is, it modifies the original list. Thus the modifications are visible everywhere that list is used.
When you do `x = x + [0]`, you rebind the name `x` to a new list (formed by adding the old list plus `[0]`). This does not modify the original list, so other places that use that list will not see the change.
If you search around for Python questions on "variables", "names", "values", "call by refernce" and the like you will find other discussions of this. Here is a question discussing a similar issue in the context of function-parameter binding. The principles are the same in for-loop variable binding.
Problem
Here are several list: a, b, etc I want to make some change of them respectively, but I'm confused with the behavier of for loop. for example: if we do ``` a, b = range(5), range(5,10) for x in [a, b]: x += [0] print(a,b) ``` we get ``` ([0, 1, 2, 3, 4, 0], [5, 6, 7, 8, 9, 0]) ``` a,b are modified. but if we do ``` a, b = range(5), range(5,10) for x in [a, b]: x = x + [0] print(a,b) ``` we get ``` ([0, 1, 2, 3, 4], [5, 6, 7, 8, 9]) ``` a,b aren't modified. I'm confused, what's the relation between x and a? When or how I can modify the value of a with x? And by the way, what's the difference between a+=b and a=a+b? Anyway, I find a solution that we can do like this ``` a, b = range(5), range(5,10) lis = [a, b] for i, x in enumerate(lis): lis[i] = ... ``` then we can modify values of a & b. But this method need make a extra list. And there's anther solution ``` for x in ['a', 'b']: exec(x + '=' + x + '+ ...') ``` And an easier solution ``` a, b = range(5), range(5,10) for x in [a, b]: x[:] = x + [0] print(a,b) ``` We will find a,b are modified :)