Open In App

Intersect Two Dictionaries through Keys - Python

Last Updated : 11 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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