Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print current value and next value during iteration in Python [duplicate]

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
like image 985
ccwhite1 Avatar asked Oct 12 '22 09:10

ccwhite1


1 Answers

The easiest way I found was:

a = ['a','b','c','d','e']

for i,nexti in zip(a,a[1::]):
    print i,nexti
like image 142
P2bM Avatar answered Oct 18 '22 09:10

P2bM