Search This Blog

Python: remove dictionary items while iterating it

  • Delete the matching items based on the keys:
    for k in dict.keys():
        if k == 'yes':
        del dict[k]
    
  • Delete the matching items based on the values:
    for k, v in dict.items():
        if v == 3:
            del dict[k]
    

No comments:

Post a Comment