python - incrementing key,value pair inside for loop iterating over dictionary -
how can increment key value pair inside loop while iterating on dictionary in python?
for key,value in mydict.iteritems(): if condition: #inc key,value #something
i have tried using next(mydict.iteritems())
, mydict.iteritems().next()
in vain.
i using python2.7.
edit - know forloop automatically increments key,value pair. want manually increment next pair ( resulting in 2 increments). can't use continue because want both key value pair accessible @ same time. when increment, have current key value pair next. if use continue, lose access current , next key,value pair.
do mean this?
mydict = {'a': 1, 'b': 2, 'c': 3} items = mydict.iteritems() key,value in items: if key == 'b': # condition try: key,value = next(items) except stopiteration: break print(key, value)
output:
('a', 1) ('c', 3)
Comments
Post a Comment