Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Intersect two dictionaries through keys

Python | Intersect two dictionaries through keys

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




# Python code to demonstrate
# intersection of two dictionaries
# using dict comprehension
 
# initialising dictionary
ini_dict1 = {'nikhil': 1, 'vashu' : 5,
             'manjeet' : 10, 'akshat' : 15}
ini_dict2 = {'akshat' :15, 'nikhil' : 1, 'me' : 56}
 
# printing initial json
print ("initial 1st dictionary", ini_dict1)
print ("initial 2nd dictionary", ini_dict2)
 
# intersecting two dictionaries
final_dict = {x:ini_dict1[x] for x in ini_dict1
                              if x in ini_dict2}
 
# printing final result
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




# Python code to demonstrate
# intersection of two dictionaries
# using dict comprehension
 
# initialising dictionary
ini_dict1 = {'nikhil': 1, 'vashu' : 5,
             'manjeet' : 10, 'akshat' : 15}
ini_dict2 = {'akshat' :15, 'nikhil' : 1, 'me' : 56}
 
# printing initial json
print ("initial 1st dictionary", ini_dict1)
print ("initial 2nd dictionary", ini_dict2)
 
# intersecting two dictionaries
final_dict = dict(ini_dict1.items() & ini_dict2.items())
 
# printing final result
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




# Initialize dictionaries
dict1 = {'nikhil': 1, 'vashu': 5,
         'manjeet': 10, 'akshat': 15}
dict2 = {'akshat': 15, 'nikhil': 1, 'me': 56}
 
 
# Find intersection of dictionaries
intersection = {i: dict1[i]
                for i in set(dict1.keys()).intersection(set(dict2.keys()))}
 
print(intersection)
# This code is contributed by Edula Vinay Kumar Reddy


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




# Python code to demonstrate
# intersection of two dictionaries
# using set and items method
 
# initialising dictionary
ini_dict1 = {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}
ini_dict2 = {'akshat': 15, 'nikhil': 1, 'me': 56}
 
# printing initial dictionaries
print("Initial 1st dictionary:", ini_dict1)
print("Initial 2nd dictionary:", ini_dict2)
 
# intersecting two dictionaries
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}
 
# printing final result
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

RELATED ARTICLES

Most Popular

Recent Comments