Open In App

Python | Set Difference in list of dictionaries

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The difference of two lists have been discussed many times, but sometimes we have a large number of data and we need to find the difference i.e the elements in dict2 not in 1 to reduce the redundancies. Let’s discuss certain ways in which this can be done. 

Method #1 : Using list comprehension The naive method to iterate both the list and extract the difference can be shortened to the method in which we shorten the code and increase the readability using list comprehension. 

Python3




# Python3 code to demonstrate
# set difference in dictionary list
# using list comprehension
 
# initializing list
test_list1 = [{"HpY" : 22}, {"BirthdaY" : 2}, ]
test_list2 = [{"HpY" : 22}, {"BirthdaY" : 2}, {"Shambhavi" : 2019}]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " +  str(test_list2))
 
# using list comprehension
# set difference in dictionary list
res = [i for i in test_list1 if i not in test_list2] \
      + [j for j in test_list2 if j not in test_list1]
 
# printing result
print ("The set difference of list is : " +  str(res))


Output :

The original list 1 is : [{‘HpY’: 22}, {‘BirthdaY’: 2}] The original list 2 is : [{‘HpY’: 22}, {‘BirthdaY’: 2}, {‘Shambhavi’: 2019}] The set difference of list is : [{‘Shambhavi’: 2019}]

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using itertools.filterfalse() This is a different way in which this particular task can be performed using the in built python function. The filterfalse method filters the not present element of one list with respect to other. 

Python3




# Python3 code to demonstrate
# set difference in dictionary list
# using itertools.filterfalse()
import itertools
 
# initializing list
test_list1 = [{"HpY" : 22}, {"BirthdaY" : 2}, ]
test_list2 = [{"HpY" : 22}, {"BirthdaY" : 2}, {"Shambhavi" : 2019}]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " +  str(test_list2))
 
# using itertools.filterfalse()
# set difference in dictionary list
res = list(itertools.filterfalse(lambda i: i in test_list1, test_list2)) \
    + list(itertools.filterfalse(lambda j: j in test_list2, test_list1))
 
# printing result
print ("The set difference of list is : " +  str(res))


Output :

The original list 1 is : [{‘HpY’: 22}, {‘BirthdaY’: 2}] The original list 2 is : [{‘HpY’: 22}, {‘BirthdaY’: 2}, {‘Shambhavi’: 2019}] The set difference of list is : [{‘Shambhavi’: 2019}]

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n*n) additional space of size n is created where n is the number of elements in the res list 



Similar Reads

Convert a List of Dictionaries into a Set of Dictionaries
Python's versatility allows developers to manipulate data in various ways. When working with a list of dictionaries, there might be scenarios where you want to convert it into a set of dictionaries to eliminate duplicates or for other reasons. In this article, we'll explore three different methods to achieve this goal with code examples. Convert A
3 min read
Python - Convert Dictionaries List to Order Key Nested dictionaries
Given list of dictionaries, convert to ordered key dictionary with each key contained dictionary as its nested value. Input : test_list = [{"Gfg" : 3, 4 : 9}, {"is": 8, "Good" : 2}] Output : {0: {'Gfg': 3, 4: 9}, 1: {'is': 8, 'Good': 2}} Explanation : List converted to dictionary with index keys. Input : test_list = [{"is": 8, "Good" : 2}] Output :
6 min read
Python Program to extract Dictionaries with given Key from a list of dictionaries
Given a list of dictionaries, the task is to write a python program that extracts only those dictionaries that contain a specific given key value. Input : test_list = [{'gfg' : 2, 'is' : 8, 'good' : 3}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}, {'love' : 3}], key= "gfg"Output : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg' : 1, 'for' : 10, 'geeks' : 9}] Expl
6 min read
Convert Dictionary of Dictionaries to Python List of Dictionaries
Dictionaries are powerful data structures in Python, allowing the storage of key-value pairs. Sometimes, we encounter scenarios where we have a dictionary of dictionaries, and we need to convert it into a list of dictionaries for easier manipulation or processing. In this article, we'll explore five different methods to achieve this conversion, eac
3 min read
Python - Convert List of Dictionaries to List of Lists
Sometimes, while working with Python data, we can have a problem in which we need to convert the list of dictionaries into a list of lists, this can be simplified by appending the keys just once if they are repetitive as mostly in records, this saves memory space. This type of problem can have applications in the web development domain. Let's discu
7 min read
Python - Convert list of dictionaries to Dictionary Value list
Given a list of dictionary, convert to dictionary with same key mapped with all values in as value list. Input : test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, {"Gfg" : 8, "is" : 11, "best" : 19}] Output : {'Gfg': [6, 8], 'is': [9, 11], 'best': [10, 19]} Explanation : 6, 8 of "Gfg" mapped as value list, similarly every other. Input : test_list =
10 min read
Python - Sort dictionaries list by Key's Value list index
Given list of dictionaries, sort dictionaries on basis of Key's index value. Input : [{"Gfg" : [6, 7, 8], "is" : 9, "best" : 10}, {"Gfg" : [2, 0, 3], "is" : 11, "best" : 19}, {"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}], K = "Gfg", idx = 0 Output : [{'Gfg': [2, 0, 3], 'is': 11, 'best': 19}, {'Gfg': [4, 6, 9], 'is': 16, 'best': 1}, {'Gfg': [6, 7, 8],
14 min read
Python - Convert List to List of dictionaries
Given list values and keys list, convert these values to key value pairs in form of list of dictionaries. Input : test_list = ["Gfg", 3, "is", 8], key_list = ["name", "id"] Output : [{'name': 'Gfg', 'id': 3}, {'name': 'is', 'id': 8}] Explanation : Values mapped by custom key, "name" -> "Gfg", "id" -> 3. Input : test_list = ["Gfg", 10], key_li
5 min read
Filter List of Python Dictionaries by Key in Python
Filtering a list of dictionaries in Python based on a specific key is a common task in data manipulation and analysis. Whether you're working with datasets or dictionaries, the ability to extract relevant information efficiently is crucial. In this article, we will explore some simple and commonly used methods to filter a list of dictionaries in Py
3 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read