[Tutor] list initialization question
Tim Condit
Tim Condit <timc@ans.net>
Mon, 3 Apr 2000 18:18:28 +0000 (GMT)
Hello,
I just want to say a quick thanks for all the help I've gotten on my
question. Not only did I get very prompt responses, but they were
consistent too! This is one of the things I really like about Python..
the clarity and consistency. (A lot of the time) I can focus on the
problem I'm trying to solve, and the language lets me do it. The rest of
the time, there is a friendly user community that is very helpful. :)
Thanks,
Tim
On Mon, 3 Apr 2000 alan.gauld@bt.com wrote:
> > Why does this fail...
> >
> > >>> x = []
>
> Here you create an empty list - ie. no members
>
> > >>> for i in range(11):
> > ... x[i] = whrandom.randint(1, 100)
>
> here you try to access the ith element of x
> - which doesn't exist yet....
>
> Try x.append(....) instead.
>
> > ... but this succeeds?
> >
> > >>> x = range(11)
>
> Here you create a list with 11 members
>
> > >>> for i in range(11):
> > ... x[i] = whrandom.randint(1, 100)
>
> and access one of its members.
>
> > Is it necessary to not only initialize a list, but also to
> > populate it?
>
> Yes, or use append.
>
> Alan G.
>