Python dictionaries list find duplicates based on values

I have a list of dicts in python 2.7.

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

I would like to go through the list and find those dicts that have the same id value (e.g. id = 1) and create a new dict

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

I hope I was clear enough.

Thanks so much for your suggestions.

+5
source share
3 answers

It is better to store the values ​​of "desc" as lists everywhere, even if they contain only one element. So you can do

for d in b:
    print d['id']
    for desc in d['desc']:
        print desc

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

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

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

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

b Now:

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

You can try:

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)]
+9
source
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 if len(desc) > 1 else desc[0])
     for id, desc in d.items()]

:

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

All Articles