how to iterate over several lists?

Chris Rebert clp2 at rebertia.com
Fri Jun 5 06:13:18 CEST 2009


On Thu, Jun 4, 2009 at 9:07 PM, kj<no.email at please.post> 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?  I'm thinking
> of something in the same vein as Perl's:
>
> for $x in (@list_a, @list_b) {
>  foo($x);
> }

Just add the lists together.

for x in list_a + list_b:
    foo(x)

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list