How to delete items from a dictionary while iterating over it?

前端 未结 10 1318
一生所求
一生所求 2020-11-22 17:22

Is it legitimate to delete items from a dictionary in Python while iterating over it?

For example:

for k, v in mydict.iteritems():
   if k == val:
           


        
10条回答
  •  感情败类
    2020-11-22 17:30

    Iterate over a copy instead, such as the one returned by items():

    for k, v in list(mydict.items()):
    

提交回复
热议问题