Possible Duplicate: Iterate a list as pair (current, next) in Python
While iterating a list, I want to print the current item in the list, plus the next value in the list.
listOfStuff = [a,b,c,d,e]
for item in listOfStuff:
print item, <nextItem>
The output would be:
a b
b c
c d
d e
The easiest way I found was:
a = ['a','b','c','d','e']
for i,nexti in zip(a,a[1::]):
print i,nexti
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With