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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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