list comprehensions value assignment syntax error

Hans Nowak wurmy at earthlink.net
Thu Jan 9 18:18:26 EST 2003


Peter Abel wrote:

> Hans Nowak <wurmy at earthlink.net> wrote in message news:<3E1B74F8.2020200 at earthlink.net>...
> 
>>   l = []
>>   for i in range(256):
>>       l[i] = chr(i)
> 
> Sorry Hans, your last statement result in an IndexError:
> Traceback (most recent call last):
>   File "<interactive input>", line 2, in ?
> IndexError: list assignment index out of range
> 
> You can't adress an element that isn't initiated!

You're right of course. I meant to write

   l.append(chr(i))

instead of

   l[i] = chr(i)

That'll teach me to test code that I post, or at least try it in the 
interactive interpreter... >_<

Cheers,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.awaretek.com/nowak/kaa.html





More information about the Python-list mailing list