how to iterate over several lists?

Stefan Behnel stefan_ml at behnel.de
Fri Jun 5 00:52:57 EDT 2009


kj wrote:
> Suppose I have two lists, list_a and list_b, and I want to iterate
> over both as if they were a single list.  E.g. I could write:
> 
> for x in list_a:
>     foo(x)
> for x in list_b:
>     foo(x)
> 
> But is there a less cumbersome way to achieve this?

Take a look at the itertools module, especially itertools.chain().

Stefan



More information about the Python-list mailing list