How to Compare List of Dictionaries in Python Last Updated : 19 Dec, 2024 Summarize Comments Improve Suggest changes Share 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") OutputNot identical Let's explore more methods to compare list of dictionaries in Python.Table of ContentSet-based ComparisonComparing List ElementsComparing Based on Specific KeysCustom ComparisonsSet-based ComparisonSet-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") OutputIdentical Explanation:Dictionaries are converted to frozensets for hashing.Sets of frozensets are compared to check for equality.Comparing List ElementsTo 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 KeysTo 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 ComparisonsFor 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. Comment More infoAdvertise with us Next Article Iterate through list of dictionaries in Python P punam6fne Follow Improve Article Tags : Python python-list Practice Tags : pythonpython-list Similar Reads Iterate through list of dictionaries in Python In this article, we will learn how to iterate through a list of dictionaries. List of dictionaries in use: [{'Python': 'Machine Learning', 'R': 'Machine learning'}, {'Python': 'Web development', 'Java Script': 'Web Development', 'HTML': 'Web Development'}, {'C++': 'Game Development', 'Python': 'Game 3 min read How to use a List as a key of a Dictionary in Python 3? In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained 3 min read Python - Find the Common Keys from two Dictionaries In this article, we will learn how can we print common keys from two Python dictionaries. We will see that there are multiple methods one can do this task. From brute force solutions of nested loops to optimize ones that trade between space and time to decrease the time complexity we will see differ 4 min read Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to 5 min read Python | Common items among dictionaries Sometimes, while working with Python, we can come across a problem in which we need to check for the equal items count among two dictionaries. This has an application in cases of web development and other domains as well. Let's discuss certain ways in which this task can be performed. Method #1 : Us 6 min read Check if two lists are identical in Python Our task is to check if two lists are identical or not. By "identical", we mean that the lists contain the same elements in the same order. The simplest way to check if two lists are identical using the equality operator (==).Using Equality Operator (==)The easiest way to check if two lists are iden 2 min read Like