Iterating through multiple sequences

Mark Charsley mark.charsley at REMOVE_THIS.radioscape.com
Thu Oct 31 05:45:00 EST 2002


In several cases recently I've wanted to iterate through two sequences at 
the same time. The way I've been doing it so far is

    assert(len(myList1) == len(myList2))
    for i in range(len(myList1)):
        doSomethingWith(myList1[i],myList2[i])
    
which is a little ugly. Is there some clever idiom I'm missing that would 
simplify things? Something like

    for elem1,elem2 in myList1,myList2:
        doSomethingWith(elem1,elem2)
        
which, while valid python, doesn't do what I hoped for

TIA

Mark




More information about the Python-list mailing list