[Tutor] list initialization question
Tim Condit
timc@ans.net
Mon, 3 Apr 2000 13:58:48 +0000 (GMT)
Hello again,
On Mon, 3 Apr 2000, Tim Condit wrote:
>
> Greetings,
>
> Why thus?
>
> Why does this fail...
>
> >>> x = []
> >>> for i in range(11):
> ... x[i] = whrandom.randint(1, 100)
> ... print i, x[i]
> ...
> Traceback (innermost last):
> File "<stdin>", line 2, in ?
> IndexError: list assignment index out of range
>
>
> ... but this succeeds?
>
> >>> x = range(11)
> >>> x
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> >>> for i in range(11):
> ... x[i] = whrandom.randint(1, 100)
> ... print i, x[i]
> ...
> 0 96
> 1 38
> 2 20
> 3 52
> 4 60
> 5 7
> 6 46
> 7 78
> 8 5
> 9 81
> 10 89
>
>
> Is it necessary to not only initialize a list, but also to populate it? Or
> am I doing something wrong (I hope so..)? If you can add, delete, etc.
> items to a list, then why am I getting the error "list assignment index
> out of range"?
>
> Thanks!
> Tim
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>