newbie question
Chris Rebert
clp2 at rebertia.com
Fri Apr 1 17:02:49 EDT 2011
On Fri, Apr 1, 2011 at 12:52 PM, Karl <8213543GGXNVJXACA at kabelmail.de> wrote:
> Hello,
>
> one beginner question:
>
> aList = [0, 1, 2, 3, 4]
>
> bList = [2*i for i in aList]
Equivalently: bList = [0, 2, 4, 6, 8]
> sum = 0
>
> for j in bList:
Note that you're iterating over *b*List here, as opposed to *a*List. I
think you made a typo and intended "for j in aList:" instead.
> sum = sum + bList[j]
>
> print j
>
> 0
>
> 2
>
> 4
>
> IndexError: 'list index out of range'
>
> Why is j in the second run 2 and not 1 in the for-loop?? I think j is a
> control variable with 0, 1, 2, 3, ...
Your code, as written, iterates over the *elements* of *b*List, which,
consistent with the behavior you're observing, are [0, 2, 4, 6, 8]. j
will therefore equal 0, then 2, then 4, etc...
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list