Python | Difference in keys of two dictionaries
Last Updated :
31 Aug, 2023
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)
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)
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)
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
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)
OutputDifference 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)
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.
Similar Reads
Python | Set Difference in list of dictionaries
The difference of two lists have been discussed many times, but sometimes we have a large number of data and we need to find the difference i.e the elements in dict2 not in 1 to reduce the redundancies. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension The na
3 min read
Python - Symmetric Difference of Dictionaries
Given two Dictionaries, the task is to write a Python program to get the symmetric difference. Examples: Input : test_dict1 = {'Gfg' : 4, 'is' : 3, 'best' : 7, 'for' : 3, 'geek' : 4}, test_dict2 = {'Gfg' : 4, 'is' : 3, 'good' : 7, 'for' : 3, 'all' : 4} Output : {'all': 4, 'good': 7, 'best': 7, 'geek
6 min read
Python | Merging two list of dictionaries
Given two list of dictionaries, the task is to merge these two lists of dictionaries based on some value. Merging two list of dictionariesUsing defaultdict and extend to merge two list of dictionaries based on school_id. Python3 # Python code to merge two list of dictionaries # based on some value.
6 min read
Intersect Two Dictionaries through Keys - Python
The task is to intersect two dictionaries through their keys, which means finding the keys that are common to both dictionaries.For example, given the dictionaries a = {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15} and b = {'akshat': 15, 'nikhil': 1, 'me': 56}, the goal is to find the inters
4 min read
Get Total Keys in Dictionary - Python
We are given a dictionary and our task is to count the total number of keys in it. For example, consider the dictionary: data = {"a": 1, "b": 2, "c": 3, "d": 4} then the output will be 4 as the total number of keys in this dictionary is 4.Using len() with dictThe simplest way to count the total numb
2 min read
Combine keys in a list of dictionaries in Python
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform a merge of dictionaries in list with similar keys. This kind of problem can come in data optimization domains. Let's discuss a way in which this task can be performed. Input : test_list = [{'a': 6},
2 min read
How to Print Dictionary Keys in Python
We are given a dictionary and our task is to print its keys, this can be helpful when we want to access or display only the key part of each key-value pair. For example, if we have a dictionary like this: {'gfg': 1, 'is': 2, 'best': 3} then the output will be ['gfg', 'is', 'best']. Below, are the me
2 min read
Difference between two Lists in Python
The difference between two lists in Python refers to the elements that are present in one list but not in the other. For example, finding the difference between lists a = [1, 2, 3, 4] and b = [3, 4, 5, 6] can result in [1, 2] by removing the common elements (3 and 4).Using setSet operations are most
3 min read
Get the First Key in Dictionary - Python
We are given a dictionary and our task is to find the first key in the dictionary. Since dictionaries in Python 3.7+ maintain insertion order, the first key is the one that was added first to the dictionary. For example, if we have the dictionary {'a': 10, 'b': 20, 'c': 30}, the first key is 'a'.Usi
2 min read
Key Index in Dictionary - Python
We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp
2 min read