Iteration over lists, Python Programming

 

What if you had a list of integer values, and you need to add  them  up and give the sum?  Here are a number of different types of doing  it.

First,  here  is a type in a style  you  may  have  learned to write  in a Java class (actually, you would have used  for, but Python does not have a for that works  like the one in C and Java).

 

def addList1(l):

sum  = 0

listLength = len(l)

i =  0

while (i < listLength):

sum  = sum + l[i]

i =  i + 1 return sum

 

It increments the index i from 0 through the length of the list - 1, and includes the appropriate components of the list into the sum.  This is perfectly right, but pretty verbose and easy to get wrong.

Here is a method of version using  Python's for loop.

 

def addList2(l):

sum  = 0

for i in range(len(l)):

sum  = sum + l[i]

return sum

 

A loop of the form

 

for x in l: something will be executed once for each element in the structure l, with the variable x having each successive element in l on each iteration. So,for  x in range(3): print x will print  0 1 2. Back to addList2,  we look that i will take on variables  from 0 to the length  of the list minus 1, and on every iteration, it will include the appropriate component from l into the addition.  This is more compact and simpler to get right than  the ?rst method of version, but still not the good one  we can do!

 

This one is even more direct.

 

def addList3(l):

sum  = 0

for  v in l:

sum  = sum + v return sum

 

We do not ever really need to work with the indices.  Here, the internal variable v gets on each successive integer  in l, and those values  are goes into sum.

 

For the truly  lazy, it turns out that  the function we need  is already built  into Python. It is known as sum:

 

def addList4(l):

return sum(l)

 

Posted Date: 8/9/2012 7:20:38 AM | Location : United States







Related Discussions:- Iteration over lists, Assignment Help, Ask Question on Iteration over lists, Get Answer, Expert's Help, Iteration over lists Discussions

Write discussion on Iteration over lists
Your posts are moderated
Related Questions
Primitives, Composition, Abstraction, and Patterns   We will  start  by thinking about  how  the  PCAP  framework applies to computer programs, in general. We may do that by

Every programming language has to have  some method for indicating grouping of operations. Here is how you execute an if-then-else structure in Java:   if (s == 1){ s = s

draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

In English, specify a representation of the board game in Python. The representation should capture the entire state of the game at one point in time. It may be helpful to first fi

#ques Write the pseudocode (use a word processor please) and the Python 3.0 program for the following problem. A monkey is being fed some food. Read in the starting weight in lbs:o

Your task is to construct a translator from the subset of Python in Project 3 intocontinuation-passing style (CPS). Of course, the expected way of accomplishing this is to trans

A program to count how many files are on the file system which displays summary information regarding the total number of bytes used by all files and a breakdown of the number of b

I need how to write small kind of viruses...

Procedures as first-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable