Saturday, 15 August 2015

python 2.7 - Find the next item of a dictionary in a for loop using .iteritems() -



python 2.7 - Find the next item of a dictionary in a for loop using .iteritems() -

is possible next iteritem in loop loop is

for x,y dict.iteritems()

where i'm interested in next item of x without moving iterator forward.

in python 2.7.x

this more or less duplicate of questions how access previous/next element while looping? , iterate list pair (current, next) in python .

note follows solely adaption of most upvoted answer in first thread, adapted fit specification:

def iterate(iterable): iterator = iter(iterable) item = iterator.next() next_item in iterator: yield item, next_item item = next_item yield item, none

usage:

d = {'a': 1, 'b': 2, 'c': 3} item, next_item in iterate(d.iteritems()): print line, '| next:', nextline

credit due markus jarderot solution.

python-2.7 dictionary iterator

No comments:

Post a Comment