[Tutor] List weirdness

bob gailer bgailer at gmail.com
Fri Feb 13 18:01:39 CET 2009


Moos Heintzen wrote:
> Hi,
> I was wondering why this happens. I was trying to create a list of lists.
>
> >>> d = [[]]
> >>> d[0][0]=1
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> IndexError: list assignment index out of range
> >>> d
> [[]]
>
> What's wrong with that?
>
> However:
> >>> d[0].append(1)
> >>> d
> [[1]]
>
> I guess I can't reference [0] on an empty list. (I come from a C 
> background.)
Can you have an empty array in C? If so, it does not have any elements, 
so you can't refer to element 0.

-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list