Open In App

How to Compare List of Dictionaries in Python

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Comparing a list of dictionaries in Python involves checking if the dictionaries in the list are equal, either entirely or based on specific key-value pairs. This process helps to identify if two lists of dictionaries are identical or have any differences. The simplest approach to compare two lists of dictionaries when the order needs to be identical.

Python
a = [{'id': 1, 'name': 'Ram'}, {'id': 2, 'name': 'Mohan'}]
b = [{'id': 1, 'name': 'Shyam'}, {'id': 2, 'name': 'sohan'}]

if a == b:
    print("Identical")
else:
    print("Not identical")

Output
Not identical

Let's explore more methods to compare list of dictionaries in Python.

Set-based Comparison

Set-based comparison efficiently checks if two unordered lists of dictionaries are identical by converting them to frozensets for fast set operations.

Python
a = [{'id': 1, 'name': 'Ram'}, {'id': 2, 'name': 'Mohan'}]
b = [{'id': 2, 'name': 'Mohan'}, {'id': 1, 'name': 'Ram'}]

set_a = {frozenset(d.items()) for d in a} 
set_b = {frozenset(d.items()) for d in b}  

print("Identical" if set_a == set_b else "Not identical")

Output
Identical

Explanation:

  • Dictionaries are converted to frozensets for hashing.
  • Sets of frozensets are compared to check for equality.

Comparing List Elements

To find dictionaries that are present in both lists, you can use a list comprehension. This method checks each dictionary in the first list to see if it exists in the second list.

Python
a = [{'id': 1, 'name': 'Ram'}, {'id': 2, 'name': 'Mohan'}]
b = [{'id': 2, 'name': 'Sohan'}, {'id': 3, 'name': 'Shyam'}]

res = [dict1 for dict1 in a if dict1 in b]
print(res)

Output
[]

Explanation:

  • Compares dictionaries in a with b and stores the common ones in res.
  • Prints the common dictionaries between a and b.

Comparing Based on Specific Keys

To compare Sometimes, you may want to compare dictionaries based on specific keys, such as 'id'. To do this, you can extract the values of these keys and compare them based on specific keys (e.g., 'id').

Example:

Python
a = [{'id': 1, 'name': 'Ram'}, {'id': 2, 'name': 'Mohan'}]
b = [{'id': 2, 'name': 'Sohan'}, {'id': 3, 'name': 'Shyam'}]

c = {item['id'] for item in a}  # Set from a
d = {item['id'] for item in b}  # Set from b
res = c.intersection(d)  # Common IDs
print(res)

Output
{2}

Custom Comparisons

For more complex comparisons, we might need custom logic. Here’s an example of how you can write a function to compare dictionaries with custom rules.

Python
def fun(dict1, dict2):
    return dict1['id'] == dict2['id'] and dict1['name'].lower() == dict2['name'].lower()

a = [{'id': 1, 'name': 'Ram'}, {'id': 2, 'name': 'Mohan'}]
b = [{'id': 2, 'name': 'sohan'}, {'id': 3, 'name': 'Shyam'}]
res = [d1 for d1 in a for d2 in b if fun(d1, d2)]  # Compare
print(res)

Output
[]

Explanation:

  • Compares dictionaries in a and b based on matching id and case-insensitive name.
  • Prints dictionaries from a that match any in b.

Next Article
Article Tags :
Practice Tags :

Similar Reads