[Django]-Count the duplicates in list of dict in django python

3๐Ÿ‘

โœ…

set(tuple(sorted(r.iteritems())) for r in result)

gives you a set of unique elements in this list. Take its length and compare it to len(result).

To get each element and its count within the result:

counter = {}
for r in result:
    tup = tuple(sorted(r.iteritems()))
    counter[tup] = counter.get(tup, 0) + 1
for tup, cnt in counter.iteritems():
    print dict(tup), cnt

prints:

{'object_id': 2, 'content_type': 42} 1
{'object_id': 1, 'content_type': 46} 2
๐Ÿ‘คeumiro

6๐Ÿ‘

If Iโ€™m understanding your request correctly, collections.Counter would be a useful way to count the duplicates. It works only with hashable inputs, so you dictionaries in your list need to be converted tuples of sorted items:

>>> from collections import Counter
>>> Counter([tuple(sorted(d.items())) for d in result])
Counter({(('content_type', 46), ('object_id', 1)): 2, (('content_type', 42), ('object_id', 2)): 1})

It probably goes without saying that the duplicates are the entries with counts greater than one ๐Ÿ™‚

Leave a comment