I want to check if an entire dictionary (both the key and value) exists in a list of dictionaries. Each dictionary can be a nested dictionary of dictionaries and lists.
When I have many scalars that I want to check if each scalar exists within a target list of scalars, I usually make the target list into a set and check existence in the set, like scalar in set(list_of_scalars)
. (Please let me know if this is already not the best way to have been doing this)
For dicts, I can't do my_dict in set(list_of_dicts)
because that raises unhashable type: 'dict'
.
Doing my_dict in list_of_dicts
seems to correctly returns False
if the same key name exists but value is different (which is what I want), but I'm concerned about the runtime; does python optimize this internally? What else can I be doing?
EDIT: Assume I will be performing MANY lookups, and using Python3.7