def filter_dict(my_dict, number): """ Filters all entries in my_dict with values less than or equal to number """ for key in my_dict.keys(): if my_dict[key] <= number: del my_dict[key] my_dict.keys() returns a new list of the keys and avoids the problem of trying to iterate of a dictionary that we are modifying.