Remove an item from a dictionary when its key is unknown

前端 未结 10 1259
悲哀的现实
悲哀的现实 2020-11-29 19:12

What is the best way to remove an item from a dictionary by value, i.e. when the item\'s key is unknown? Here\'s a simple approach:

for key, item in some_di         


        
10条回答
  •  再見小時候
    2020-11-29 19:50

    This is how I would do it.

    for key in some_dict.keys():
        if some_dict[key] == item_to_remove:
            some_dict.pop(key)
            break
    

提交回复
热议问题