Question on for loop
steve+comp.lang.python at pearwood.info
Fri Jan 4 06:48:24 CET 2013
On Thu, 03 Jan 2013 12:04:03 -0800, subhabangalore wrote:
> Dear Group,
> If I take a list like the following:
> fruits = ['banana', 'apple', 'mango']
> for fruit in fruits:
> print 'Current fruit :', fruit
> if I want variables like var1,var2,var3 be assigned to them, we may
> take, var1=banana,
> but can we do something to assign the variables dynamically
Easy as falling off a log. You can't write "var1", "var2" etc. but you
can write it as "var", "var" etc.
var = ['banana', 'apple', 'mango']
print var # prints 'banana'
print var # prints 'apple'
print var # prints 'mango'
Of course "var" is not a very good variable name. "fruit" or "fruits"
would be better.
More information about the Python-list