Ahsan
Ahsan

Reputation: 11832

Count the duplicates in list of dict in django python

How can i find the count of duplicates in this list.

>>> result = SiteTags.objects.values('content_type','object_id')
>>> result
[{'object_id': 1, 'content_type': 46}, {'object_id': 1, 'content_type': 46}, {'object_id': 2, 'content_type': 42}]

Is there anyway to find in query? or through an other way?

Thanks!

Upvotes: 3

Views: 598

Answers (2)

Raymond Hettinger
Raymond Hettinger

Reputation: 226171

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 :-)

Upvotes: 6

eumiro
eumiro

Reputation: 212825

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

Upvotes: 3

Related Questions