Skip to content Skip to sidebar Skip to footer

Python List Of Dictionaries Find Duplicates Based On Value

I have a list of dicts: a =[{'id': 1,'desc': 'smth'}, {'id': 2,'desc': 'smthelse'}, {'id': 1,'desc': 'smthelse2'}, {'id': 1,'desc': 'smthelse3'}] I would like to go tr

Solution 1:

You can try:

importoperator, itertools

key = operator.itemgetter('id')

b = [{'id': x, 'desc': [d['desc'] for d in y]} 
     for x, y in itertools.groupby(sorted(a, key=key), key=key)]

Solution 2:

It is better to keep the "desc" values as lists everywhere even if they contain a single element only. This way you can do

fordinb:
    printd['id']fordescind['desc']:
        printdesc

This would work for strings too, just returning individual characters, which is not what you want.

And now the solution giving you a list of dicts of lists:

a =[{'id': 1,'desc': 'smth'},{'id': 2,'desc': 'smthelse'},{'id': 1,'desc': 'smthelse2'},{'id': 1,'desc': 'smthelse3'}]

c = {}
fordin a:
    c.setdefault(d['id'], []).append(d['desc'])
b = [{'id': k, 'desc': v} fork,v in c.iteritems()]

b is now:

[{'desc': ['smth', 'smthelse2', 'smthelse3'], 'id': 1},
 {'desc': ['smthelse'], 'id': 2}]

Solution 3:

from collections import defaultdict

d = defaultdict(list)
for x in a:
    d[x['id']].append(x['desc']) # group description by id
b = [dict(id=id, desc=desc iflen(desc) > 1else desc[0])
     forid, desc in d.items()]

To preserve order:

b = []
foridin (x['id'] for x in a):
    desc = d[id]
    if desc:
       b.append(dict(id=id, desc=desc iflen(desc) > 1else desc[0]))
       del d[id]

Post a Comment for "Python List Of Dictionaries Find Duplicates Based On Value"