[Tutor] Alternative to nested loops

Steve Nelson sanelson at gmail.com
Sun Mar 19 22:05:37 CET 2006


Hi All,

I had a feeling I could do this:

>>> foo
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> for c in foo:
...     for b in c:
...             print b
...
1
2
3
1
2
3
1
2
3

Using a list comprehension, as it seemed to me like I was saying: b
for c in foo, but I can't see how to do this.  Ultimately I want to
sum each number and produce a total.  I know how to do this as above,
but was wondering if there is an alternative / better way?

S.


More information about the Tutor mailing list