Let's say we have a dict
dict = {
"a": "x",
"b": "x",
"c": "x",
"d": "y",
"e": "y",
"f": "y",
}
How do I quickly check, if a set of specific keys all have the same value?
E.g.:
list_of_keys = ["a", "b", "c"]
should return True.
list_of_keys = ["b", "c", "d"]
should return False
.
Thanks in advance.
You can use built-in all
function and compare all values to the first one:
all(dict[k] == dict[list_of_keys[0]] for k in list_of_keys)
Another suggestion (from the comments):
len(set(dict[k] for k in list_of_keys)) == 1
Sidenote: don't use dict
as a variable name. It's python's built-in.