Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43258
| References | <4c47da9e-c632-4855-98a7-0506d8013046@googlegroups.com> |
|---|---|
| Date | 2013-04-10 18:52 +1000 |
| Subject | Re: use a loop to create lists |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.401.1365583968.3114.python-list@python.org> (permalink) |
On Wed, Apr 10, 2013 at 6:40 PM, <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)
They don't need unique names. Just use the same name inside the loop:
global_list=[]
for i in range(20):
current_list=[] # Presumably you do something with it here
global_list.append(current_list)
That'll work fine, and you can reference the lists by their positions
in global_list.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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