Question on for loop

Discussion in 'Python' started by subhabangalore@gmail.com, Jan 3, 2013.

  1. Guest

    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.
    , Jan 3, 2013
    #1
    1. Advertising

  2. MRAB Guest

    On 2013-01-03 20:04, 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.
    MRAB, Jan 3, 2013
    #2
    1. Advertising

  3. Peter Otten Guest

    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.


    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
    Peter Otten, Jan 3, 2013
    #3
  4. Matt Jones Guest

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

    > On 2013-01-03 20:04, 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>
    >
    Matt Jones, Jan 3, 2013
    #4
  5. Don Ross Guest

    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

    On Thursday, January 3, 2013 2:04:03 PM UTC-6, 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.
    Don Ross, Jan 3, 2013
    #5
  6. alex23 Guest

    On Jan 4, 6:04 am, wrote:
    > 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.
    alex23, Jan 4, 2013
    #6
  7. 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
    >
    > 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.




    --
    Steven
    Steven D'Aprano, Jan 4, 2013
    #7
  8. Guest

    On Friday, January 4, 2013 11:18:24 AM UTC+5:30, Steven D'Aprano wrote:
    > 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

    >
    > >

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


    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.
    , Jan 15, 2013
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,379
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,228
    Tim Slattery
    Mar 30, 2005
  3. takayuki
    Replies:
    2
    Views:
    278
    Calvin Spealman
    Jun 16, 2008
  4. takayuki
    Replies:
    17
    Views:
    471
    John Salerno
    Jun 17, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    370
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page