Timothy Tran Timothy Tran - 8 months ago 30
Python Question

Best Way to find Identical Key-Value pairs in List of Dictionaries Python

If I have a list of dictionaries such as

[{'D': 'wet', 'W': 'sun'}, {'D': 'wet', 'W': 'rain'}, {'D': 'wet', 'W': 'sun'}]

What would be the best way to detect if there is a Key-Value pair that is identical between all dictionaries in the list. So in this case, "D" : "Wet" would be identical because it is the same across all the Key and Values of the list of dictionaries. However, the "W" will not be because it has more a >1 domain (sun and rain are both in the domain of the W key).


you can also take advantage of the intersection property built into sets

dict(set.intersection(*(set(d.items()) for d in dicts)))