Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!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.096 X-Spam-Evidence: '*H*': 0.82; '*S*': 0.01; '[0,': 0.09; 'subject:create': 0.09; 'python': 0.11; 'received:141': 0.14; 'brackets:': 0.16; 'dict': 0.16; 'goebel': 0.16; 'simplified': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'header:User-Agent:1': 0.23; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'didnt': 0.31; 'occurs': 0.31; "we're": 0.32; 'maybe': 0.34; 'subject:lists': 0.35; 'but': 0.35; 'list': 0.37; 'lists.': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'header :Message-Id:1': 0.63; 'different': 0.65; 'thomas': 0.65; 'header :Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'miss': 0.74; 'square': 0.74; 'as:': 0.81; '2013': 0.98 Date: Thu, 11 Apr 2013 16:33:21 +0200 From: Thomas Goebel Organization: Technische Hochschule Nuernberg User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 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> <51656F6A.5040103@th-nuernberg.de> <5166A297.70104@th-nuernberg.de> <5166B345.8080200@th-nuernberg.de> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Thomas.Goebel@ohm-hochschule.de 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365690806 news.xs4all.nl 2659 [2001:888:2000:d::a6]:43595 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43374 * On 11/04/2013 16:11, Franz Kelnreiter wrote: > On Thu, Apr 11, 2013 at 2:57 PM, Thomas Goebel < > Thomas.Goebel@ohm-hochschule.de> wrote: > >> ... >> Which is the same as: >> f = {'list_' + str(n):[m for m in range(3)] for n in range(3)} >> > [...] > But didnt you miss square brackets: > > f = {'list_' + str(n):[[m for m in range(3)] for n in range(3)]} If i try your code i get a dict with key 'list_2' which value is a list of three lists. Maybe this occurs because we're using different python versions?! I never used python<2.7! Have you tried this one d2 = dict(('list_' + str(i), list(range(3))) for i in range(3)) with Chris' simplification: * On 11/04/2013 15:22, Chris Angelico wrote: > On Thu, Apr 11, 2013 at 10:57 PM, Thomas Goebel wrote: >> [a for a in range(3)] >> >> will return a list >> [0, 1, 2] > > Simplification possible: That's the same as: > > list(range(3)) > >> f = {'list_' + str(n):[m for m in range(3)] for n in range(3)} > > Meaning that this can be simplified too: > > f = {'list_' + str(n):list(range(3)) for n in range(3)}