Question on for loop

Matt Jones matt.walker.jones at gmail.com
Thu Jan 3 21:28:06 CET 2013


Yeah, this seems like a bad idea.  What exactly are you trying to do here?

Maybe using a dictionary is what you want?


d = {
    'first' : 'banana',
    'second' : 'apple',
    'third'  : 'mango'
}

for key, value in d.items():
    print key, value


However I'm still not sure why you'd want to do this.

*Matt Jones*


On Thu, Jan 3, 2013 at 2:21 PM, MRAB <python at mrabarnett.plus.com> wrote:

> On 2013-01-03 20:04, subhabangalore at gmail.com wrote:
>
>> Dear Group,
>> If I take a list like the following:
>>
>> fruits = ['banana', 'apple',  'mango']
>> for fruit in fruits:
>>     print 'Current fruit :', fruit
>>
>> Now,
>> if I want variables like var1,var2,var3 be assigned to them, we may take,
>> var1=banana,
>> var2=apple,
>> var3=mango
>>
>> but can we do something to assign the variables dynamically I was thinking
>> of
>> var_series=['var1','var2','**var3']
>> for var in var_series:
>>    for fruit in fruits:
>>         print var,fruits
>>
>> If any one can kindly suggest.
>>
>> Regards,
>> Subhabrata
>>
>> NB: Apology for some alignment mistakes,etc.
>>
>>  Why would you want to do that? Creating names dynamically like that is
> a bad idea. Just keep them in a list, like they are already.
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130103/a5a2a460/attachment.html>


More information about the Python-list mailing list