Intersect Two Dictionaries through Keys - Python
Last Updated :
11 Jul, 2025
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 intersection of these dictionaries, meaning the key-value pairs that exist in both dictionaries. The result will be {'nikhil': 1, 'akshat': 15}, which includes only the key-value pairs that are common to both a and b.
Using set intersection
In Python, sets provide efficient operations like intersection which is ideal for finding common dictionary keys. By converting dictionary keys to sets and using the intersection method, we can quickly identify shared keys, making this approach highly suitable for large datasets due to constant time complexity .
Python
a = {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}
b = {'akshat': 15, 'nikhil': 1, 'me': 56}
c = set(a.keys()) # set of keys from 'a'
d = set(b.keys()) # set of keys from 'b'
e = c.intersection(d) # intersection of the two sets
res = {k: a[k] for k in e}
print(res)
Output{'nikhil': 1, 'akshat': 15}
Explanation: res = {k: a[k] for k in e} creates a new dictionary by iterating over the common keys in e and fetching their corresponding values from dictionary a.
Using dictionary comprehension
Another approach for intersecting dictionary keys is using dictionary comprehension. This method checks if each key in one dictionary is present in the other dictionary. It's useful for smaller dictionaries.
Python
a = {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}
b = {'akshat': 15, 'nikhil': 1, 'me': 56}
res = {k: a[k] for k in a if k in b}
print(res)
Output{'nikhil': 1, 'akshat': 15}
Explanation:
- {k: a[k] for k in a if k in b} iterates over the keys of dictionary a and for each key k, it checks if k is also present in dictionary b.
- If the key exists in both dictionaries, it adds the key-value pair from a to the result dictionary res.
Using filter()
filter() with a lambda expression is another functional programming approach to intersect dictionary keys. This method filters the key-value pairs of the first dictionary where the key also exists in the second dictionary.
Python
a = {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}
b = {'akshat': 15, 'nikhil': 1, 'me': 56}
res = dict(filter(lambda item: item[0] in b, a.items()))
print(res)
Output{'nikhil': 1, 'akshat': 15}
Explanation:
- filter() filters the items of a by applying the lambda function, which checks if the key of each key-value pair is present in dictionary b.
- If the key is found in b, the item is included in the result and then
dict()
converts the filtered result into a dictionary .
Using list comprehension
List comprehension is another way to intersect two dictionaries. We can first generate a list of matching keys by iterating over the keys of one dictionary and checking if they exist in the other dictionary. After finding the common keys, we can construct a new dictionary using these keys.
Python
a = {'nikhil': 1, 'vashu': 5, 'manjeet': 10, 'akshat': 15}
b = {'akshat': 15, 'nikhil': 1, 'me': 56}
c = [key for key in a if key in b]
res = {key: a[key] for key in c}
print(res)
Output{'nikhil': 1, 'akshat': 15}
Explanation:
- list comprehension creates a list of keys that are present in both dictionaries a and b . It checks each key from dictionary a and includes it in the list c if it also exists in dictionary b.
- {key: a[key] for key in c} constructs a new dictionary res, where the keys are those in the list c and the values are fetched from dictionary a based on those keys.
Similar Reads
Iterate Through Dictionary Keys And Values In Python In Python, a Dictionary is a data structure where the data will be in the form of key and value pairs. So, to work with dictionaries we need to know how we can iterate through the keys and values. In this article, we will explore different approaches to iterate through keys and values in a Dictionar
2 min read
Python | Difference in keys of two dictionaries 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: k
5 min read
Iterate Through Specific Keys in a Dictionary in Python Sometimes we need to iterate through only specific keys in a dictionary rather than going through all of them. We can use various methods to iterate through specific keys in a dictionary in Python.Using dict.get() MethodWhen we're not sure whether a key exists in the dictionary and don't want to rai
3 min read
Python | Filter Tuple Dictionary Keys Sometimes, while working with Python dictionaries, we can have itâs keys in form of tuples. A tuple can have many elements in it and sometimes, it can be essential to get them. If they are a part of a dictionary keys and we desire to get filtered tuple key elements, we need to perform certain functi
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
Merge Dictionaries without Overwriting in Python Merging dictionaries in Python is a common operation. In this article, we will see how we can append two dictionaries so that they don't overwrite each other in Python. Append two dictionaries so they don't Overwrite Each OtherBelow are some of the ways by which we can append two dictionaries in Pyt
3 min read