I sometimes need to iterate a list in Python looking at the \"current\" element and the \"next\" element. I have, till now, done so with code like:
for curre
Pairs from a list using a list comprehension
the_list = [1, 2, 3, 4] pairs = [[the_list[i], the_list[i + 1]] for i in range(len(the_list) - 1)] for [current_item, next_item] in pairs: print(current_item, next_item)
Output:
(1, 2) (2, 3) (3, 4)