I am interested to know some details about how ... in works in Python.
My understanding of for var in iterable at each iteration creates a var variable that is bound to the current iteration value. So, if you do for c in cows; c = cows[whatever] for c in cows; c = cows[whatever] , but changing c in the loop does not affect the original value. However, it looks like this works differently if you assign a value to a dictionary key.
cows=[0,1,2,3,4,5] for c in cows: c+=2 #cows is now the same - [0,1,2,3,4,5] cows=[{'cow':0},{'cow':1},{'cow':2},{'cow':3},{'cow':4},{'cow':5}] for c in cows: c['cow']+=2 # cows is now [{'cow': 2}, {'cow': 3}, {'cow': 4}, {'cow': 5}, {'cow': 6}, {'cow': 7} #so, it changed the original, unlike the previous example
I see that you can use an enumeration to make the first example work, but this, in my opinion, is a different story.
cows=[0,1,2,3,4,5] for i,c in enumerate(cows): cows[i]+=1 # cows is now [1, 2, 3, 4, 5, 6]
Why does this affect the initial values of the list in the second example, but not the first?
[edit]
Thanks for answers. I looked at it from a PHP perspective, where you can use the and symbol in foreach to indicate if you are working with a link or a copy of an iteration. Now I see that the real difference is the main detail of how python works with respect to immutable objects.