-1

Maybe I'm too spoiled with the usual awesomeness of Python, but is there a more natural way to iterate over intervals of a list?

Instead of:

L = [12, 15, 29, 100, 239]

for i in range(len(L)-1):
    print L[i], L[i+1]

12 15
15 29
29 100
100 239

is there somthing like this:

for i, j in intervals(L): 
   print i, j

?

Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

4

Yes, you can use zip:

for i, j in zip(L, L[1:]): 
   print i, j
Basj
  • 41,386
  • 99
  • 383
  • 673
Francisco
  • 10,918
  • 6
  • 34
  • 45