Python | Set Difference in list of dictionaries
Last Updated :
27 Apr, 2023
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
Python | Difference in keys of two dictionaries In this article, we will be given two dictionaries dic1 and dic2 which may contain the same keys and we have to find the difference of keys in the given dictionaries using Python. Example Input: dict1= {'key1':'Geeks', 'key2':'For', 'key3':'geeks'}, dict2= {'key1':'Geeks', 'key2':'Portal'} Output: k
5 min read
Python - Symmetric Difference of Dictionaries Given two Dictionaries, the task is to write a Python program to get the symmetric difference. Examples: Input : test_dict1 = {'Gfg' : 4, 'is' : 3, 'best' : 7, 'for' : 3, 'geek' : 4}, test_dict2 = {'Gfg' : 4, 'is' : 3, 'good' : 7, 'for' : 3, 'all' : 4} Output : {'all': 4, 'good': 7, 'best': 7, 'geek
6 min read
Difference between two Lists in Python The difference between two lists in Python refers to the elements that are present in one list but not in the other. For example, finding the difference between lists a = [1, 2, 3, 4] and b = [3, 4, 5, 6] can result in [1, 2] by removing the common elements (3 and 4).Using setSet operations are most
3 min read
Removing Dictionary from List of Dictionaries - Python We are given a list of dictionaries, and our task is to remove specific dictionaries based on a condition. For instance given the list: a = [{'x': 10, 'y': 20}, {'x': 30, 'y': 40}, {'x': 50, 'y': 60}], we might want to remove the dictionary where 'x' equals 30 then the output will be [{'x': 10, 'y':
3 min read
Python | Difference in Record Lists Sometimes, while working with data, we may have a problem in which we require to find the difference records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
5 min read
Python - Sort Dictionary by Value Difference Sometimes, while working with Python dictionaries, we can have problem in which in which we need to perform sorting of items on basis of various factors. One such can be on basis of absolute difference of dual value list. This can occur in Python > 3.6, as dictionaries are ordered. This kind of p
3 min read