Finding Commonalities in Two Dictionaries

Problem

  • You have two dictionaries and want to find out what they might have in common (same keys, same values, etc.).

Solution

  • Perform common set operations using the keys() or items() methods

In [1]:
a = {
'x' : 1,
'y' : 2,
'z' : 3 }

b = {
'w' : 10,
'x' : 11,
'y' : 2 }

In [2]:
# Find keys in common
a.keys() & b.keys() # { 'x', 'y' }


Out[2]:
{'x', 'y'}

In [3]:
# Find keys in a that are not in b 
a.keys() - b.keys() # { 'z' }


Out[3]:
{'z'}

In [4]:
# Find (key,value) pairs in common 
a.items() & b.items() # { ('y', 2) }


Out[4]:
{('y', 2)}
  • These kinds of operations can also be used to alter or filter dictionary contents. For example, suppose you want to make a new dictionary with selected keys removed.

In [6]:
# Make a new dictionary with certain keys removed
c = {key:a[key] for key in a.keys() - {'z', 'w'}} 
# c is {'x': 1, 'y': 2}
print(c)


{'y': 2, 'x': 1}