Issue
I would like to do something like the following:
x = 1
y = 2
z = 3
l = [x,y,z]
for variable in l:
variable += 2
print x
print y
print z
Unfortunately, when I print x,y and z after this block, the values have remained at 1,2 and 3 respectively.
I have looked at this related question, but the solution there doesn't solve my problem.
Is there a neat and efficient way to perform the same action to multiple variables like I have tried to above?
P.S. I am using Python 2.6
Solution
You cant change x,y,z like this. x,y,z are integers and they are immutable. You need to make new variables.
x, y, z = (v + 2 for v in l)
Answered By - Marcin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.