A little more advanced for loop

Diez B. Roggisch deets at nospam.web.de
Fri Feb 9 05:57:53 EST 2007


Horta wrote:

>     Hi folks,
> 
>   Suppose I have to loop over 3 lists being the same size at the same
> time and order. How can I do that without using the range() function
> or whatever indexing?
> 
> Example using range:
> 
> a = ['aaa', 'aaaa']
> b = ['bb', 'bbbb']
> c = ['c', 'cccc']
> 
> for i in range(len(a)):
>     # using a[i], b[i], and c[i]
> 
>   I'm sure there's a elegant way to do that...


Use zip:

for av, bv, cv in zip(a, b, c):
    print av, bv, cv

Diez



More information about the Python-list mailing list