Lists of lists traversal

Dan Schmidt dfan at harmonixmusic.com
Mon Dec 20 17:54:58 EST 1999


"Benjamin Dixon" <beatle at arches.uga.edu> writes:

| Hello, I am new to Python and am trying to figure out how I can iterate
| over a list that I know to contain other lists of integers so that I can
| add up the individual lists inside the larger list.
| 
| I tried things like this:
| 
| Sum(input):
| 	for x in input:
| 		value = 0
| 		for y in input:
| 			value = value + y
| 	return y
| 
| and other stuff but I'm not certain as to how to reference a specific
| member of a given sublist.

 - You're missing a 'def' in the first line.
 - I think you mean 'for y in x' instead of 'for y in input'.
 - You probably want to initialize value to 0 outside the x loop, not
   inside it.
 - I imagine you mean to return value, not y.

Other than that it looks okay.

-- 
Dan Schmidt | http://www.dfan.org



More information about the Python-list mailing list