[Tutor] Iterating over two sequences in "parallel"
Jose Amoreira
ljmamoreira at gmail.com
Sat Nov 28 10:52:38 CET 2009
Hi!
I want to process corresponding elements of two lists, sequentially. Call the
lists list1 and list2, and assume they have equal lengths. I can do something
like
for index in range(len(list1)):
process(list1[index], list2[index])
But I find it somehow rather ugly, because we generate yet another an list for
the index, when we already have the two we want to process. I know we can use
xrange, but still I find it awkward...
Instead of the above snippet, I am considering something like
while list1:
process(list1.pop(), list2.pop())
But this has the side effect of emptying both lists, which may not be
convenient. Of course we can make backup copies of the lists if needed, but we
are then recovering the previous method ugliness...
Do you guys have any suggestions regarding this? Thanks
Jose Amoreira
More information about the Tutor
mailing list