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


Groups > comp.lang.python > #43369

Re: Fwd: use a loop to create lists

Date 2013-04-11 15:43 +0200
From Thomas Goebel <Thomas.Goebel@ohm-hochschule.de>
Organization Technische Hochschule Nuernberg
Subject Re: Fwd: use a loop to create lists
References (1 earlier) <51656F6A.5040103@th-nuernberg.de> <CAOjpqbPmjO1YS2N+931Q6+0NkHALnFocBydBtnhRxjHDf_d-DQ@mail.gmail.com> <5166A297.70104@th-nuernberg.de> <CAOjpqbM80RTXQsxgLJ9R_5X8uezWZQKcZ3F_fN8_vuq1D7_JoA@mail.gmail.com> <CAOjpqbMnZ_17iDj12qFqKU8sTEm7-QdYQAgi5T21R7k6kXFHgg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.473.1365687787.3114.python-list@python.org> (permalink)

Show all headers | View raw


* On 11/04/2013 14:11, Franz Kelnreiter wrote:
> On Thu, Apr 11, 2013 at 1:46 PM, Thomas Goebel wrote:
> 
>> global_list = {'_'.join(['list', str(i)]):[] for i in range(20)}
>
> Thanks for your explanation, I think I know what you want to do and I would
> very much like to understand your code in detail - maybe I am too stupid -
> but when I execute the value part of your code construct:
> 
> [a for a in range(20)] for i in range(20)
> 
> I get a syntax error, as I exepected (Python 2.6.4 (r264:75708, Oct 26
> 2009, 08:23:19)).

Sorry Franz,

as you are using python 2.6 you have to use

d1 = dict(('list_' + str(i), []) for i in range(3))
d2 = dict(('list_' + str(i), [m for m in range(3)]) for i in range(3))

like stated here:
http://stackoverflow.com/questions/1747817/python-create-a-dictionary-with-list-comprehension

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