# Fwd: Re: [Tutor] Issues with initializing lists

steve lonetwin@yahoo.com
Fri, 10 Aug 2001 20:11:11 +0530

```Hi there,
I don't know why what you said is happening, is happening...!! It looks
mighty strange, though with list comprehensions you can get what you want=
=2E..
code:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

>>> l =3D  [ [None]*3 for x in range(0,4) ]
>>> l

[[None, None, None], [None, None, None], [None, None, None], [None, None,
None]]

>>> l[1][1] =3D 'foo'
>>> l

[[None, None, None], [None, 'foo', None], [None, None, None], [None, None=
,
None]]

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
hope that helps....and hope some of the gurus explain this deep mystery s=
oon
=2E..:)

Peace
Steve

> Hi,
> I am a bit confused about the usage of lists in Python. For an applicat=
ion
> I am writing, I would like to create a list of lists with some default
> values, and then fill in certain items in the list later.
>
> As an example:
> >>> l1=3D[[None]*3]*4
> >>> l1
>
> [[None, None, None], [None, None, None], [None, None, None], [None, Non=
e,
> None]]
>
> Now, suppose I want to fill in the second item in the second sublist. I=
try
>
> >>> l1[1][1]=3D'hello'
>
> Then 'l1' looks like
>
> >>> l1
>
> [[None, 'hello', None], [None, 'hello', None], [None, 'hello', None],
> [None, 'hello', None]]
>
> Note how 'hello' has propagated to each sublist.
>
> Why is this?
>
>
> I could do
>
> l1=3D[]
>
> >>> for i in range(4):
>
> ... =09l1.append([])
> ... =09for j in range(3):
> ... =09=09l1[i].append(None)
> ...
> l1[1][1]=3D'hello'
>
> >>> l1
>
> [[None, None, None], [None, 'hello', None], [None, None, None], [None,
> None, None]]
>
> This looks fine. But this approach really seems excessive.
>
> My real question is how do I conveniently initialize such a list so tha=
t I
> can later fill in the sublists?

-----------------------------------------
bug, n:
=09=09A son of a glitch.
-----------------------------------------

-------------------------------------------------------

--=20
-----------------------------------------
bug, n:
=09=09A son of a glitch.
-----------------------------------------

```