Home > OS >  Find duplicate elements in list of dictionaries & output values in rang
Find duplicate elements in list of dictionaries & output values in rang

Time:01-20

I've a list of dictionaries like this.

x=({'apple': 5, 'mango': 3,'banana': 2}, {'apple': 6, 'mango': 1})

I want to find the find the duplicate elements in sub-dictionary & range values with key. Example.

Expected outputs

{'apple': [5, 6], 'mango': [1, 3], 'banana': [2, 2]}

After applying some basics. I've worked on this problem as follows.

import collections
result = collections.defaultdict(list)

for i, d in enumerate(x):
    result[frozenset(d.items())].append(i)
print(result)

Output I got.

defaultdict(list,
            {frozenset({('apple', 5), ('banana', 2), ('mango', 3)}): [0],
             frozenset({('apple', 6), ('mango', 1)}): [1]})

As I mentioned before my expected output should be list of duplicate key range values. Can you guys help me what are the steps/Logic should I apply so that I can get expected outputs as mentioned above.

CodePudding user response:

(i) use dict.setdefault to combine the list of dictionaries

(ii) sort the values of the dictionary from (i) to obtain the range values

out = {}
for d in x:
    for k,v in d.items():
        out.setdefault(k, []).append(v)

for k,v in out.items():
    if len(v) > 1:
        v.sort()
        out[k] = [v[0], v[-1]]
    else:
        out[k] = [v[0], v[0]]

Output:

{'apple': [5, 6], 'mango': [1, 3], 'banana': [2, 2]}

CodePudding user response:

You have given tuple. But, Consider that your source data is a list.

x=[{'apple': 5, 'mango': 3,'banana': 2}, {'apple': 6, 'mango': 1}]

sourceDict = {}
for i in x:
    for key,value in i.items():
        if not key in sourceDict:
            sourceDict.update({key:[value]})
        else:
            sourceDict[key].append(value)

sourceDict output:

{'apple': [5, 6], 'mango': [3, 1], 'banana': [2]}

Incase if you want them to sorted,

for i,j in sourceDict.items():
    sourceDict[i] = sorted(j)

which gives,

{'apple': [5, 6], 'mango': [1, 3], 'banana': [2]}
  •  Tags:  
  • Related