Given two dictionaries, the task is to find the intersection of these two dictionaries through keys. Let’s see different ways to do this task.
Method #1: Using dict comprehension
Python3
ini_dict1 = { 'nikhil' : 1 , 'vashu' : 5 ,
'manjeet' : 10 , 'akshat' : 15 }
ini_dict2 = { 'akshat' : 15 , 'nikhil' : 1 , 'me' : 56 }
print ("initial 1st dictionary", ini_dict1)
print ("initial 2nd dictionary", ini_dict2)
final_dict = {x:ini_dict1[x] for x in ini_dict1
if x in ini_dict2}
print ("final dictionary", str (final_dict))
|
Output:
initial 1st dictionary {‘vashu’: 5, ‘manjeet’: 10, ‘nikhil’: 1, ‘akshat’: 15} initial 2nd dictionary {‘nikhil’: 1, ‘me’: 56, ‘akshat’: 15} final dictionary {‘nikhil’: 1, ‘akshat’: 15}
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.
Method #2: Using & operator
Python3
ini_dict1 = { 'nikhil' : 1 , 'vashu' : 5 ,
'manjeet' : 10 , 'akshat' : 15 }
ini_dict2 = { 'akshat' : 15 , 'nikhil' : 1 , 'me' : 56 }
print ("initial 1st dictionary", ini_dict1)
print ("initial 2nd dictionary", ini_dict2)
final_dict = dict (ini_dict1.items() & ini_dict2.items())
print ("final dictionary", str (final_dict))
|
Output:
initial 1st dictionary {‘vashu’: 5, ‘manjeet’: 10, ‘nikhil’: 1, ‘akshat’: 15} initial 2nd dictionary {‘nikhil’: 1, ‘akshat’: 15, ‘me’: 56} final dictionary {‘nikhil’: 1, ‘akshat’: 15}
Method #3: Using intersection method
Python3
dict1 = { 'nikhil' : 1 , 'vashu' : 5 ,
'manjeet' : 10 , 'akshat' : 15 }
dict2 = { 'akshat' : 15 , 'nikhil' : 1 , 'me' : 56 }
intersection = {i: dict1[i]
for i in set (dict1.keys()).intersection( set (dict2.keys()))}
print (intersection)
|
Output
{'akshat': 15, 'nikhil': 1}
This approach first converts the keys of each dictionary into sets using the set function, and then uses the intersection method to find the common keys. It then creates a new dictionary using a dictionary comprehension, with the common keys as the keys and the corresponding values from dict1 as the values.
The time complexity of this approach would be O(n) where n is the number of keys in the dictionaries. This is because we are iterating through all the keys in both dictionaries and performing the intersection operation.
The space complexity of this approach would be O(n) as well, since we are creating a new dictionary with n keys in it.
Method #3: Using the built-in set function and the items method.
Here’s a step-by-step approach:
- Initialize the two dictionaries:
- Create a set of keys in each dictionary:
- Find the intersection of the two sets:
- Create a new dictionary with the key-value pairs from ini_dict1 that have keys in the intersection_set:
- Print the final dictionary
Below is the implementation of the above approach:
Python3
ini_dict1 = { 'nikhil' : 1 , 'vashu' : 5 , 'manjeet' : 10 , 'akshat' : 15 }
ini_dict2 = { 'akshat' : 15 , 'nikhil' : 1 , 'me' : 56 }
print ( "Initial 1st dictionary:" , ini_dict1)
print ( "Initial 2nd dictionary:" , ini_dict2)
set1 = set (ini_dict1.keys())
set2 = set (ini_dict2.keys())
intersection_set = set1.intersection(set2)
final_dict = {k: ini_dict1[k] for k in intersection_set}
print ( "Final dictionary:" , final_dict)
|
Output
Initial 1st dictionary: {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}
Initial 2nd dictionary: {'akshat': 15, 'nikhil': 1, 'me': 56}
Final dictionary: {'akshat': 15, 'nikhil': 1}
Time complexity: O(n), where n is the number of keys in the larger dictionary.
Auxiliary space: O(n), where n is the number of keys in the intersection set
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Apr, 2023
Like Article
Save Article