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 :)