How to use frozenset for Removing duplicate dictionaries in a list In Python
frozenset is used to assign a value to key in dictionary as a set. The repeated entries of dictionary are hence ignored and hence solving this particular task.
# using frozenset
# initializing list
test_list = [{"Akash" : 1}, {"Kil" : 2}, {"Akshat" : 3},
{"Kil" : 2}, {"Akshat" : 3}]
# printing original list
print ("Original list : " + str(test_list))
# using frozenset to
# remove duplicates
res_list = {frozenset(item.items()) :
item for item in test_list}.values()
# printing resultant list
print ("Resultant list is : " + str(res_list))
Output:
Original list : [{'Akash': 1}, {'Kil': 2}, {'Akshat': 3}, {'Kil': 2}, {'Akshat': 3}]
Resultant list is : [{'Kil': 2}, {'Akshat': 3}, {'Akash': 1}]
Python | Removing duplicate dicts in list
Removal of duplicates is essential in many applications. A list of dictionaries is quite common and sometimes we require to duplicate the duplicated. Let’s discuss certain ways in which we can remove duplicate dictionaries in a list in Python.
Contact Us