<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
On 04/01/2011 12:52 PM, Karl wrote:
<blockquote cite="mid:in5ado$ipk1ドル@news-cedar.fernuni-hagen.de"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1038.35">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; background-color: #f0f0f0}
p.p4 {margin: 0.0px 0.0px 13.0px 0.0px; font: 13.0px 'Trebuchet MS'}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 12.0px Monaco}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 12.0px Monaco; min-height: 16.0px}
span.s1 {color: #e44515}
span.Apple-tab-span {white-space:pre}
</style>
<p class="p1">Hello,</p>
<p class="p2"><br>
</p>
<p class="p1">one beginner question:</p>
<p class="p2"><br>
</p>
<p class="p1">aList = [0, 1, 2, 3, 4]</p>
<p class="p1">bList = [2*i for i in aList]</p>
<p class="p1">sum = 0</p>
<p class="p1">for j in bList:</p>
<p class="p1"><span class="Apple-tab-span"> </span>sum = sum +
bList[j]</p>
</blockquote>
<br>
Your j is already an element of bList. You don't need to index
bList again. Instead do<br>
for b in bList:<br>
sum = sum+b<br>
<br>
<blockquote cite="mid:in5ado$ipk1ドル@news-cedar.fernuni-hagen.de"
type="cite">
<p class="p1"><span class="Apple-converted-space"> </span>print
j</p>
<p class="p2"><br>
</p>
<p class="p3">0</p>
<p class="p3">2</p>
<p class="p3">4</p>
<p class="p4"><span class="s1"><b>IndexError:</b></span> 'list
index out of range'</p>
<p class="p5">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, ...</p>
</blockquote>
No, it's not a control variable, it's the actual elements of the
list.<br>
<br>
<br>
If you want a control variable as an index you can do <br>
for j in range(len(bList)):<br>
sum = sum + bList[j]<br>
<br>
But that is less efficient <br>
<br>
<br>
<blockquote cite="mid:in5ado$ipk1ドル@news-cedar.fernuni-hagen.de"
type="cite">
<p class="p6"><br>
</p>
<p class="p5">Thanks!</p>
<p class="p6"><br>
</p>
<p class="p5">Karl</p>
</blockquote>
<br>
</body>
</html>