删除字典中具有特定值的键

时间:2021-05-17 10:07:31

标签: python dictionary

我正在尝试删除其值列表中包含“F”的键 (102),如下所示

grades = {101:['A','HD'],102:['D','F'],103:['B','CR'],104:['B','CR'],105:['A','HD'],106:['C','P']}

for k,v in grades.items():
    if 'F' in v: # if v[1] == 'F':
        del grades[k] # grades.pop(k)

我收到错误

RuntimeError: dictionary changed size during iteration

如何在不遇到该错误的情况下删除它?

0 个答案:

没有答案