Open In App

Python | Difference in keys of two dictionaries

Last Updated : 31 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be given two dictionaries dic1 and dic2 which may contain the same keys and we have to find the difference of keys in the given dictionaries using Python.

Example

Input: dict1= {'key1':'Geeks', 'key2':'For', 'key3':'geeks'}, 
           dict2= {'key1':'Geeks', 'key2':'Portal'}
Output: key3
Explanation: key1 and key2 is already present in both the dictionaries and key3 isn't 
                        present in dict2. So, key3 is the difference in keys in dict1 and dict2.

Difference in keys of two dictionaries

Below are the different ways by which we can find the difference in keys in two dictionaries:

  • Using Set
  • Finding keys in dict2 that are not in dict1
  • Finding keys in dict1 that are not in dict2. 
  • Using inbuilt method
  • Using re module
  • Finding the same keys in two dictionaries

Difference in Keys of Two Dictionaries Using Set

In this example, we are using a set to find the key difference in which we take the set of dict1 and dict2 subtract them from each other, and return that difference in the form of the key as shown in the below code.

Python3




# Initialising dictionary
dict1= {'key1':'Geeks', 'key2':'For', 'key3':'geeks'}
dict2= {'key1':'Geeks', 'key2':'Portal'}
 
diff1 = set(dict2) - set(dict1)
diff2= set(dict1) - set(dict2)
 
print(diff1)
print(diff2)


Output

set()
{'key3'}

Time complexity: O(n), where n is the maximum number of keys in both dictionaries.
Auxiliary space: O(m), where m is the number of keys in the intersection of both dictionaries.

Finding keys in dict2 which are not in dict1

In this example, we are finding the keys that are present in dict2 but not in dict1 by using a for loop for iterating and if condition for testing this condition.

Python3




# Python code to find difference in keys in two dictionary
 
# Initialising dictionary
dict1= {'key1':'Geeks', 'key2':'For'}
dict2= {'key1':'Geeks', 'key2':'For', 'key3':'geeks',
        'key4': {'GeekKey1': 12, 'GeekKey2': 22, 'GeekKey3': 32 }}
 
for key in dict2.keys():
    if not key in dict1:
 
        # Printing difference in
        # keys in two dictionary
        print(key)


Output

key3
key4

Time complexity: O(n), where n is the maximum number of keys in both dictionaries.
Auxiliary space: O(m), where m is the number of keys in the intersection of both dictionaries.

Finding keys in dict1 which are not in dict2

In this example, we are finding the keys that are present in dict1 but not in dict2 by using a for loop for iterating and if condition for testing this condition.

Python3




# Initialising dictionary
dict1= {'key1':'Geeks', 'key12':'For'}
dict2= {'key1':'Geeks', 'key2':'For', 'key3':'geeks',
        'key4': {'GeekKey1': 12, 'GeekKey2': 22, 'GeekKey3': 32 }}
         
for key in dict1.keys():
    if not key in dict2:
 
        # Printing difference in
        # keys in two dictionary
        print(key)


Output

key12

Time complexity: O(n), where n is the maximum number of keys in both dictionaries.
Auxiliary space: O(m), where m is the number of keys in the intersection of both dictionaries.

Difference in Keys Using Inbuilt Method

In this example, we are finding the key difference by using the inbuilt method difference() that are that finds the difference between keys in two different dictionaries.

Python3




# Initializing dictionaries
dict1 = {'key1': 'Geeks', 'key2': 'For', 'key3': 'geeks'}
dict2 = {'key1': 'Geeks', 'key2': 'Portal'}
 
# Finding the keys that are missing in dict2 compared to dict1
diff = set(dict1.keys()).difference(dict2.keys())
 
# Printing the difference in keys
print(diff)
#This code is contributed by Edula Vinay Kumar Reddy


Output

{'key3'}

Time complexity: O(n), where n is the maximum number of keys in both dictionaries.
Auxiliary space: O(m), where m is the number of keys in the intersection of both dictionaries.

Finding Keys Difference Using re module

Using re module extracts the keys from the string representation of the dictionaries using a regular expression pattern r”‘(\w+)'”. It converts the resulting lists of keys to sets and finds the difference between them.In this example, we import the re-module, define two dictionaries dict1 and dict2, convert both dictionaries to strings using str() method, use the re.findall() method with a regular expression pattern r”‘(\w+)'” to extract the keys from the strings of both dictionaries, convert the resulting lists of keys to sets and find the difference between the sets of keys and print them.

Python3




import re
 
dict1 = {'key1': 1, 'key2': 2, 'key3': 3}
dict2 = {'key1': 4, 'key3': 5}
 
keys1 = set(re.findall(r"'(\w+)'", str(dict1)))
keys2 = set(re.findall(r"'(\w+)'", str(dict2)))
 
diff_keys = keys1 - keys2
 
print("Difference in keys: ", diff_keys)


Output

Difference in keys:  {'key2'}

Time complexity: O(n), where n is the maximum number of keys in both dictionaries.
Auxiliary space: O(m), where m is the number of keys in the intersection of both dictionaries.

Finding the same keys in two dictionaries

Also you can find the the keys that are present in dict2 as well as in dict1 by using a for loop for iterating and if condition for testing this condition.

Python3




# Initialising dictionary
dict1= {'key1':'Geeks', 'key2':'For'}
dict2= {'key1':'Geeks', 'key2':'For', 'key3':'geeks',
        'key4': {'GeekKey1': 12, 'GeekKey2': 22, 'GeekKey3': 32 }}
          
for key in dict1.keys():
    if  key in dict2:
        print(key)


Output

key1
key2

Time complexity: O(n), where n is the maximum number of keys in both dictionaries.
Auxiliary space: O(m), where m is the number of keys in the intersection of both dictionaries.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads