If there are not many keys whose corresponding values satisfy the condition, then you can compile the keys first and then trim the dictionary:
for k in [k for k,v in d1.items() if cond(v)]: del d1[k]
If the list [k for k,v in d1.items() if cond(v)] is too large, you can process the dictionary "in turn", i.e. collect keys until their number exceeds a threshold value, cuts off the dictionary and repeats until there are no more keys satisfying the condition:
from itertools import islice def prune(d, cond, chunk_size = 1000): change = True while change: change = False keys = list(islice((k for k,v in d.items() if cond(v)), chunk_size)) for k in keys: change = True del d[k]
source share