[Tutor] List indexing problem

Lie Ryan lie.1296 at gmail.com
Sat Jul 26 03:25:42 CEST 2008


On Sat, 2008-07-26 at 02:20 +0200, tutor-request at python.org wrote:
> Message: 8
> Date: Fri, 25 Jul 2008 19:35:54 -0400
> From: "Mike Meisner" <mikem at blazenetme.net>
> Subject: Re: [Tutor] List indexing problem
> To: "Steve Willoughby" <steve at alchemy.com>
> Cc: tutor at python.org
> Message-ID: <000501c8eeaf$2e822df0$08a305cf at Parents>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
>         reply-type=response
> 
> Thanks Steve.
> 
> It's obvious now that you've pointed it out.
> 
> Do you happen to know if there is an efficient way to initialize a
> list 
> like this without explicitly writing out each element?
> 
> Mike

A bit fragile, since it uses eval: 

lst = eval(repr([[[0]*3]*3]*10))

but in this particular case, eval won't leak harmful code (as far as I
can see) since all it process is literals, not user-inputted data.

The alternative is this:
lst = [[[0 for _ in range(3)] for _ in range(3)] for _ in range(10)]



More information about the Tutor mailing list