Remove duplicate dictionaries from a list and subtract value of keys of duplicate element

python remove duplicate values from dictionary
python list of dictionaries find duplicates based on value
python find duplicates in list of dictionaries
duplicate values in dictionary python
python list remove duplicates keep order
python remove duplicates from list
python remove duplicates from list of lists
python list of dictionaries get value

I have a list of dicts, and I'd like to remove the dicts with identical key and subtract the value pairs.

For this list:

[{'chair': 4}, {'tv': 5}, {'chair': 3}, {'tv': 2}, {'laptop': 2}]

I'd like to return this:

[{'chair': 1}, {'tv': 3}, {'laptop': 2}]

You could do it like this, creating an intermediate dict for efficiency:

dicts_list = [{'chair': 4}, {'tv': 5}, {'chair': 3}, {'tv': 2}, {'laptop': 2}]

out = {}
for d in dicts_list:
    for key, val in d.items():
       if key in out:
           out[key] -= val
       else:
           out[key] = val

out_list = [ {key:val} for key, val in out.items()]

print(out_list)
# [{'tv': 3}, {'chair': 1}, {'laptop': 2}]

But you might be interested in this intermediate dict as output:

print(out)
# {'tv': 3, 'chair': 1, 'laptop': 2}

Python, List of dictionaries are quite common and sometimes we require to duplicate the duplicated. Lets discuss frozenset is used to assign a value to key in dictionary as a set. res_list = { frozenset (item.items()) : item for item in test_list}.values(). Since the way to find uniqueness in correspondences is exactly to use a dictionary, with the desired unique value being the key, the way to go is to create a reversed dict, where your values are composed as the key - then recreate a "de-reversed" dictionary using the intermediate result.


defaultdict from collections might come in handy. This solution will cover the cases where there are more than 2 dicts of the same key in the list.

from collections import defaultdict
ls = defaultdict(list)
d = [{'chair': 4}, {'tv': 5}, {'chair': 3}, {'tv': 2}, {'laptop': 2}]


# Creating a list of all values under one key

for dic in d:
    for k in dic:
            ls[k].append(dic[k])

print(ls)
defaultdict(<class 'list'>, {'chair': [4, 3], 'tv': [5, 2], 'laptop': [2]})

# safe proofing for negative values on subtraction    
for k in ls:
    ls[k].sort(reverse=True)
    ls[k] = ls[k][0] - sum(ls[k][1:])

print(ls)
defaultdict(<class 'list'>, {'chair': 1, 'tv': 3, 'laptop': 2})

Python, The common utility to remove the dictionary corresponding to particular key in a to the introduction of No-SQL databases, which work mostly on Key-Value pairs. required key and lambda function is used to iterate to the lists elements one by Dictionaries · Python | Remove duplicate dictionaries from nested dictionary  In addition to the answer of Jon Skeet , if your value is an intern object you can use : var uniqueValues = myDict.GroupBy(pair => pair.Value.Property) .Select(group => group.First()) .ToDictionary(pair => pair.Key, pair => pair.Value); This way you will remove the duplicate only on one property of the object. share.


You can construct a defaultdict of lists, then use a list comprehension:

from collections import defaultdict

dd = defaultdict(list)
for d in data:
    k, v = next(iter(d.items()))
    dd[k].append(v)

res = [{k: v if len(v) == 1 else v[0] - sum(v[1:])} for k, v in dd.items()]

print(res)
# [{'chair': 1}, {'tv': 3}, {'laptop': [2]}]

Python : How to Remove Duplicates from a List – thispointer.com, Set is an un-ordered data structure that contains only unique elements. Now suppose we have a list that contains duplicate elements i.e.. [  Python Exercise: Remove duplicates from Dictionary Last update on February 26 2020 08:09:21 (UTC/GMT +8 hours)


Following snippet is using nothing but standard modules:

a= [{'chair': 4}, {'tv': 5}, {'chair': 3}, {'tv': 2}, {'laptop': 2}]

print("Input:", a)

b=dict()
for element in a:
    for k,v in element.items():
        try:
          # you didn't specify the subtracted element order,   
          # so I'm subtracting BIGGER from SMALLER using simple abs() :)
          b[k] = abs(b[k] - v) 
        except:
          b[k] = v

print("Output:", b)

# restore original structure
c = [ dict({item}) for item in b.items() ]

print("Output:", c)

And demo:

('Input:', [{'chair': 4}, {'tv': 5}, {'chair': 3}, {'tv': 2}, {'laptop': 2}])
('Output:', {'tv': 3, 'chair': 1, 'laptop': 2})
('Output:', [{'tv': 3}, {'chair': 1}, {'laptop': 2}])

EDIT: Added the secondary out put C to restructure B similar to A

Python : How to Remove multiple keys from Dictionary while Iterating, Now we want to remove all key/value pairs from dictionary whose value is divisible by 3. If we try to remove any element from dictionary while iterating over it then it will throw error. Iterate over the dict and put to be deleted keys in the list Python: Find duplicates in a list with frequency count & index  A List may have duplicate elements as well. To eliminate duplicates, we can use the Distinct extension method. We can use a method like ToList () to go from an IEnumerable to a List again. Distinct example. This program uses the System.Linq namespace.


How to Remove Duplicate Dictionaries in a List - Better , Create a function to remove duplicate dictionaries from a list. A dictionary is considered a duplicate if it has the exact same keys and values. What can be the fastest way to to check the duplicate values in the dictionary and print its key? Dictionary MyDict which is having following values, Key Value. 22 100. 24 200. 25 100. 26 300. 29 200. 39 400. 41 500. Example: key 22 and 25 have same values and i need to print that 22 and 25 have duplicate values.


Fundamentals of Python: First Programs, The positions range from 0 to the length of the list minus 1. • Lists An element can be replaced with a new element, added to the list, or removed from the list. The subscript operator is used to add a new key/value pair to a dictionary or to replace a value May not be copied, scanned, or duplicated, in whole or in part. The basic method that comes to mind while performing this operation is the naive method of iterating the list of dictionaries and manually removing the duplicate dictionary and append in new list. filter_none


5. Data Structures, Remove the first item from the list whose value is x. A set is an unordered collection with no duplicate elements. Performing list(d.keys()) on a dictionary returns a list of all the keys used in the dictionary, in arbitrary order (if  Given a dictionary, the task is to find keys with duplicate values. Let’s discuss a few methods for the same. Method #1: Using Naive approach