On Sat, 2008-07-26 at 02:20 +0200, [EMAIL PROTECTED] wrote:
> Message: 8
> Date: Fri, 25 Jul 2008 19:35:54 -0400
> From: "Mike Meisner" <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] List indexing problem
> To: "Steve Willoughby" <[EMAIL PROTECTED]>
> Cc: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> 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)]

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to