Intersection of Two Dictionaries via Keys in Python

In this tutorial, we are going to solve the task of intersecting two dictionaries via their keys in Python. As a result, it is necessary that there be something common between the two dictionaries.

Here, you come across a term called Python dictionaries. Dictionaries are a very common data structure of Python language.

Let us discuss dictionaries first before we dive deeper into the problem.

What are Python Dictionaries?

Python dictionaries are a mutable collection of items where the items comprise key-value pairs. Dictionary has two key elements: keys and values. These keys have to be single elements and values can be of any data type like list, string, integer, tuple, etc. The keys connect to their respective values. In other words, the values can be accessed with the help of their respective keys

In Python, multiple key-value pairs enclosed inside curly braces create a dictionary. The syntax is as follows :

dict_item = {“Name” : “Jhelum” , “Age” : 20 , “Class” : 12}

Let us see an example :

dict_item = {"Name" : "Ravi", "Age" : 25, "Salary" : 50000 , "Company" : "AMAZON", "City" : "Hyderabad"}
print(type(dict_item))
print(dict_item["Name"])

Output :

< class 'dict'>

Ravi

Python: Intersection of Two Dictionaries via Keys

In this task, we will perform the intersection of two dictionaries via their keys in Python.The intersection can be performed in 2 ways. Let’s see the approaches :

  • dict comprehension
  • & operator

In both methods, we will take the two dictionaries as default. 

Approach 1 : dict comprehension

  • First, initialize dict_1 and dict_2 with the original dictionaries respectively.
  • Print the original dictionaries.
  • Next, consider the key of dict_1 as x and run for loop to check whether the x in dict_1 exists in dict_2 as well. If it does, the common key and its value are pushed inside a new dictionary called intersect.
  • Print the new dictionary intersect with the common keys with their values.
# inititialising dictionary 
dict_1 = {'Joyjeet' : 'cricket', 'Rik' : 'hockey', 'Chetna' : 'khokho', 'Rani' : 'football'} 
dict_2 = {'Arnob' : 'baseball', 'Joyjeet' : 'cricket', 'Chetna' : 'khokho'} 

# printing original dictionary
print ("Original 1st dictionary: ", dict_1) 
print ("Original 2nd dictionary: ", dict_2) 

# intersection
intersect = {x:dict_1[x] for x in dict_1 
          if x in dict_2} 

# printing result 
print ("Intersected dictionary: ", str(intersect))

Output :

Original 1st dictionary: {'Joyjeet': 'cricket', 'Rik': 'hockey', 'Chetna': 'khokho', 'Rani': 'football'}
Original 2nd dictionary: {'Arnob': 'baseball', 'Joyjeet': 'cricket', 'Chetna': 'khokho'}
Intersected dictionary: {'Joyjeet': 'cricket', 'Chetna': 'khokho'}

Approach 2 : & operator

This approach is lot easier to understand as well as perform.

  • First, initialize the dict_1 and dict_2 with the original dictionaries.
  • Next, print the original dictionaries.
  • Next, convert the dictionaries dict_1 and dict_2 into list format using the items() function. Then, perform their AND using the & operator. The common key-value pairs are then converted into a dictionary using dict() and stored in intersect.
  • Print final dictionary intersect.
# inititialising dictionary 
dict_1 = {'Joyjeet' : 'cricket', 'Rik' : 'hockey', 'Chetna' : 'khokho', 'Rani' : 'football'}
dict_2 = {'Arnob' : 'baseball', 'Joyjeet' : 'cricket', 'Chetna' : 'khokho'} 

# printing original dictionary
print ("Original 1st dictionary", dict_1) 
print ("Original 2nd dictionary", dict_2) 

# intersecting two dictionaries 
intersect = dict(dict_1.items() & dict_2.items()) 

# printing final result 
print ("Final dictionary", str(intersect))

Output :

Original 1st dictionary {'Joyjeet': 'cricket', 'Rik': 'hockey', 'Chetna': 'khokho', 'Rani': 'football'}
Original 2nd dictionary {'Arnob': 'baseball', 'Joyjeet': 'cricket', 'Chetna': 'khokho'}
Final dictionary {'Joyjeet': 'cricket', 'Chetna': 'khokho'}

In both cases we took the dict_1 and dict_2 as {‘Joyjeet’ : ‘cricket’, ‘Rik’ : ‘hockey’, ‘Chetna’ : ‘khokho’, ‘Rani’ : ‘football’} and {‘Arnob’ : ‘baseball’, ‘Joyjeet’ : ‘cricket’, ‘Chetna’ : ‘khokho’} respectively. We can see here, that the portion ‘Joyjeet’ : ‘cricket‘ and ‘Chetna’ : ‘khokho’ are common for both. On performing both the cases, we get the same and correct output of  {‘Joyjeet’: ‘cricket’, ‘Chetna’: ‘khokho’}.

Thank you for going through this article. I hope this article was helpful enough. Please do check out the following articles :

Leave a Reply

Your email address will not be published. Required fields are marked *