Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36081

Re: Question on for loop

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'assign': 0.07; 'dynamically': 0.07; 'subject:Question': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'fruit': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.17; 'variables': 0.17; '>>>': 0.18; 'skip:v 30': 0.20; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'structure': 0.32; 'print': 0.32; 'problem': 0.33; 'to:addr :python-list': 0.33; 'another': 0.33; 'list': 0.35; 'something': 0.35; 'received:org': 0.36; 'but': 0.36; 'apple': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'group,': 0.60; 'email addr:gmail.com': 0.63; 'dear': 0.66; 'kindly': 0.67; 'take,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Question on for loop
Date Thu, 03 Jan 2013 21:22:16 +0100
Organization None
References <456df1a1-9225-44e7-9aa9-6b6fe3fde4a0@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084be63.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.58.1357244525.2939.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1357244525 news.xs4all.nl 6926 [2001:888:2000:d::a6]:35262
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36081

Show key headers only | View raw


subhabangalore@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.

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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question on for loop subhabangalore@gmail.com - 2013-01-03 12:04 -0800
  Re: Question on for loop MRAB <python@mrabarnett.plus.com> - 2013-01-03 20:21 +0000
  Re: Question on for loop Peter Otten <__peter__@web.de> - 2013-01-03 21:22 +0100
  Re: Question on for loop Matt Jones <matt.walker.jones@gmail.com> - 2013-01-03 14:28 -0600
  Re: Question on for loop Don Ross <donrosszx@gmail.com> - 2013-01-03 15:22 -0800
  Re: Question on for loop alex23 <wuwei23@gmail.com> - 2013-01-03 16:47 -0800
  Re: Question on for loop Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-04 05:48 +0000
    Re: Question on for loop subhabangalore@gmail.com - 2013-01-15 11:30 -0800
  Re: Question on for loop Alister <alister.ware@ntlworld.com> - 2013-01-04 10:16 +0000

csiph-web