Open In App

Find Common Members that are in two Lists of Dictionaries - Python

Last Updated : 06 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given two lists of dictionaries and our task is to find the common members (key-value pairs) that exist in both lists. The key-value pairs in the dictionaries may differ between the lists, but we are only interested in the common ones. For example: a = [{'apple': 10, 'banana': 20}, {'cherry': 30, 'date': 40}] and b = [{'apple': 10, 'banana': 20}, {'apple': 10, 'date': 40}] then output will be [{'apple': 10, 'banana': 20}].

Using set with frozenset Conversion

We use frozenset to convert dictionaries into immutable objects allowing them to be added to sets. By creating sets for both lists, we can use set intersection to find common dictionaries efficiently then the matching frozenset/set objects are converted back into dictionaries and returned as result using list comprehension.

Python
a = [{'apple': 10, 'banana': 20}, {'cherry': 30, 'date': 40}]
b = [{'apple': 10, 'banana': 20}, {'apple': 10, 'date': 40}]

c = [dict(d) for d in set(frozenset(d.items()) for d in a) & set(frozenset(d.items()) for d in b)]
print(c)

Output
[{'apple': 10, 'banana': 20}]

Explanation: We convert each dictionary to frozenset(d.items()) making them hashable for set operations and after finding the intersection of the sets, the matching frozenset objects are converted back into dictionaries to form the output.

Using List Comprehension

This method iterates through each dictionary in the first list and checks if it exists in the second list, only matching dictionaries are added to the result using list comprehension.

Python
a = [{'apple': 10, 'banana': 20}, {'cherry': 30, 'date': 40}]
b = [{'apple': 10, 'banana': 20}, {'apple': 10, 'date': 40}]

c = [d for d in a if d in b]
print(c)

Output
[{'apple': 10, 'banana': 20}]

Using a Hash Map

We create a hash map from the second list with frozenset representations of dictionaries as keys, this allows fast lookups to check if a dictionary from the first list exists in the second list.

Python
a = [{'apple': 10, 'banana': 20}, {'cherry': 30, 'date': 40}]
b = [{'apple': 10, 'banana': 20}, {'apple': 10, 'date': 40}]

b_map = {frozenset(d.items()): d for d in b}
c = [d for d in a if frozenset(d.items()) in b_map]
print(c)

Output
[{'apple': 10, 'banana': 20}]

Explanation: create a hash map (b_map) from b using frozenset(d.items()) as keys for fast lookups and check if frozenset(d.items()) for each dictionary in a exists in b_map.

Using filter with a Lambda Function

filter function processes each dictionary in the first list keeping only those that also exist in the second list.

Python
a = [{'apple': 10, 'banana': 20}, {'cherry': 30, 'date': 40}]
b = [{'apple': 10, 'banana': 20}, {'apple': 10, 'date': 40}]

c = list(filter(lambda d: d in b, a))
print(c)

Output
[{'apple': 10, 'banana': 20}]

Explanation: Use filter to iterate through a and retain dictionaries that exist in b. The lambda function handles the condition d in b for matching dictionaries


Similar Reads