I've written a bit of code like the following to compare items with other items further on in a list. Is there a more elegant pattern for this sort of dual iteration?
jump_item_iter = (j for j in items if some_cond)
try:
jump_item = jump_item_iter.next()
except StopIteration:
return
for item in items:
if jump_item is item:
try:
jump_item = jump_iter.next()
except StopIteration:
return
# do lots of stuff with item and jump_item
I don't think the "except StopIteration
" is very elegant
Edit:
To hopefully make it clearer, I want to visit each item in a list and pair it with the next item further on in the list (jump_item) which satisfies some_cond.