Question on for loop

S

subhabangalore

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.
 
M

MRAB

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.
 
P

Peter Otten

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.

For that problem you need another data structure -- a dictionary:
lookup_fruits = {"var1": "banana", "var2": "apple", "var3": "mango"}
var_series = ["var1", "var2", "var3"]
for var in var_series:
.... print var, lookup_fruits[var]
....
var1 banana
var2 apple
var3 mango
 
M

Matt Jones

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*


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.
 
D

Don Ross

I'm interested to know why you're trying this as well. Is this something that would be helped by creating a class and then dynamically creating instances of that class? Something like...

class Fruit:
def __init__(self, name):
self.name = name

for fruit in ['banana', 'apple', 'mango']:
varName = Fruit(fruit)
# do stuff with varName
 
A

alex23

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

Before trying to do this, write the next bit of code where you _use_
such variables. What do you do if there are no fruits? What do you do
if there are 7000?

You don't want variables to be optional, because otherwise you'll need
to guard every usage with something like:

if 'var2893' in locals(): ...

Of course, you can also automate this, but why push values into a
dictionary that exists for one purpose if you're not going to use it
that way?

If you need to deal with an unknown number of objects, use a list. If
those objects have a name by which you can refer to them, use a
dictionary.
 
S

Steven D'Aprano

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

Easy as falling off a log. You can't write "var1", "var2" etc. but you
can write it as "var[0]", "var[1]" etc.

var = ['banana', 'apple', 'mango']
print var[0] # prints 'banana'
print var[1] # prints 'apple'
print var[2] # prints 'mango'



Of course "var" is not a very good variable name. "fruit" or "fruits"
would be better.
 
S

subhabangalore

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[0]", "var[1]" etc.



var = ['banana', 'apple', 'mango']

print var[0] # prints 'banana'

print var[1] # prints 'apple'

print var[2] # prints 'mango'







Of course "var" is not a very good variable name. "fruit" or "fruits"

would be better.

Actually in many cases it is easy if you get the variable of list value, I was trying something like,
def func1(n):
list1=["x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"]
blnk=[]
for i in range(len(list1)):
num1="var"+str(i)+"="+list1
blnk.append(num1)
print blnk
Regards,
Subhabrata.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top