python - How can I filter a key out of a dictionary based on the value -


i have dict :

mydict = {'andreas': 0.4833775399766172, 'anh': nan, 'maryse': 0.61436719272499474, 'darren': -0.44898819782452443, 'jesse': 0.14565852997686479, 'mitchell': nan} 

the nan's give me no information @ want filter them out.

for k, v in mydict.iteritems():     if v == 'nan':         del mydict[v] 

i tried this, doesn't work. me? in advance.

you need use key not value , cannot delete dict iterating over, need copy:

for k, v in mydict.copy().items():     if v == "nan":         del mydict[k]  print(mydict) 

whatever nan whether string or else need use key, mydict[v] give keyerror , changing size of dict give runtimeerror: dictionary changed size during iteration


Comments

Popular posts from this blog

android - How to save instance state of selected radiobutton on menu -

python 3 IndexError: list index out of range -

IF statement in MySQL trigger -