Fraz Fraz - 1 year ago 297
Python Question

get values from all keys except one python

I have a python dictionary.
Just to give out context, I am trying to write my own simple cross validation unit.

So basically what I want is to get all the values except for the given keys..
Suppose there are 10 keys in the dict.
And depending on the input, it returns all the values from a dictionary except to those what has been given.

So if the input is 2 and 5 then the output values doesn't have the values from the keys 2 and 5?


Answer Source
for key, value in your_dict.items():
    if key not in your_blacklisted_set:
        print value

the beauty is that this pseudocode example is valid python code.

it can also be expressed as a list comprehension:

resultset = [value for key, value in your_dict.items() if key not in your_blacklisted_set]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download