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


Groups > comp.lang.python > #43272

Re: use a loop to create lists

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.100
X-Spam-Evidence '*H*': 0.80; '*S*': 0.00; 'assign': 0.07; 'list...': 0.07; 'subject:create': 0.09; 'question.': 0.14; 'bind': 0.16; "can't.": 0.16; 'elements,': 0.16; 'index.': 0.16; 'received:74.208.4.195': 0.16; 'tuple.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'string,': 0.24; 'question': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'bunch': 0.31; 'lists': 0.32; 'sense': 0.34; 'point.': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'list': 0.37; 'list.': 0.37; 'thank': 0.38; 'ahead': 0.38; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'new': 0.61; 'range': 0.61; "you'll": 0.62; 'email addr:gmail.com': 0.63; 'name': 0.63; 'real': 0.63; 'more': 0.64; 'town': 0.65; 'here': 0.66; 'believe': 0.68; 'importantly,': 0.68; 'received:74.208': 0.68; 'containing': 0.69; 'irrelevant': 0.84; 'hand,': 0.93
Date Wed, 10 Apr 2013 08:36:12 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: use a loop to create lists
References <4c47da9e-c632-4855-98a7-0506d8013046@googlegroups.com>
In-Reply-To <4c47da9e-c632-4855-98a7-0506d8013046@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:Gy/ajRmUI70BqEMI6pgE1jmivmumleAirfV+z74RcNI tZTzblHJJYT9c/GTgoM3qtoAfMbs2GwLFdr/+Ucy85U/fU1yyG anUOc9L2mwbaFelO6bn/02b4+lyoWBh9xMlGijQpJoTX76187D opS6w3ytaFXuAr+K7quq7nfliwxKkH1PEifJojZrH2MFJn4ciE a2OMJADWUuQj6IEHMU5WuVI76eb7dOKDySqqbnifBQ3z5w0iI1 y42bt3bgnT4E/Q5glQJvsMMXEHf4D2FKCWkfeISGRfOmFyxQPn X9x6pSIHX05lwF6uYyl3mNqdV19be962Gxyz9iV035A7FTHRw= =
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.412.1365597392.3114.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365597392 news.xs4all.nl 2573 [2001:888:2000:d::a6]:56213
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43272

Show key headers only | View raw


On 04/10/2013 04:40 AM, martaamunar@gmail.com wrote:
> Hi!
>
> I would like to create a list containing lists. I need each list to have a differente name and i would like to use a loop to name the list. But as the name, is a string, i cannot asign it to a value... how can I do that??
>
>
> global_list=[]
> for i in range (20):
>    ("list_"+i)=[]   #These would be the name of the list...
>    global_list.append("list_"+i)
>
> Thank you!!!!!
>

The fact that the content of the outer list also happens to be lists is 
irrelevant to your question.  I believe the real question here is how to 
create an arbitrary bunch of new names, and bind them to elements of he 
list.

In general, you can't.  But more importantly, in general you don't want 
to.  If you come up with a convoluted way to fake it, you'll have to use 
the same convoluted way to access those names, so there's no point.  As 
Chris says, just reference them by index.

On the other hand, if the outer list happens to have exactly 7 elements, 
and you know that ahead of time, then it may well make sense to assign 
names to them.  Not list_0 through list_6, but name, addr1, ec.

global_list = []
for i in range(7):
     global_list.append[]

first_name, mid_name, last_name, addr1, addr2, city, town = global_list

Incidentally, that's approximately what a collections.namedtuple is all 
about, giving names to items that are otherwise considered elements of a 
tuple.

-- 
DaveA

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


Thread

use a loop to create lists martaamunar@gmail.com - 2013-04-10 01:40 -0700
  Re: use a loop to create lists Chris Angelico <rosuav@gmail.com> - 2013-04-10 18:52 +1000
  Re: use a loop to create lists Dave Angel <davea@davea.name> - 2013-04-10 08:36 -0400
  Re: use a loop to create lists rusi <rustompmody@gmail.com> - 2013-04-10 08:31 -0700
  Re: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-10 15:55 +0200
  Fwd: use a loop to create lists Franz Kelnreiter <kelnreiter@gmail.com> - 2013-04-11 14:11 +0200
  Re: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-11 14:57 +0200
  Re: use a loop to create lists Chris Angelico <rosuav@gmail.com> - 2013-04-11 23:22 +1000
  Re: Fwd: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-11 15:43 +0200
  Re: use a loop to create lists Franz Kelnreiter <kelnreiter@gmail.com> - 2013-04-11 16:11 +0200
  Re: use a loop to create lists Thomas Goebel <Thomas.Goebel@ohm-hochschule.de> - 2013-04-11 16:33 +0200
  Re: Fwd: use a loop to create lists Franz Kelnreiter <kelnreiter@gmail.com> - 2013-04-11 16:33 +0200
  Re: use a loop to create lists Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-04-11 18:17 -0400

csiph-web