I want to make a function that returns a copy of a dictionary excluding keys specified in a list.
Considering this dictionary:
my_dict = {
"keyA": 1,
"keyB": 2,
"keyC": 3
}
A call to without_keys(my_dict, ['keyB', 'keyC'])
should return:
{
"keyA": 1
}
I would like to do this in a one-line with a neat dictionary comprehension but I'm having trouble. My attempt is this:
def without_keys(d, keys):
return {k: d[f] if k not in keys for f in d}
which is invalid syntax. How can I do this?