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 ๐
- [Django]-Django Formset Missing Management Form Data
- [Django]-Remove decimal point from number passed to django template?
- [Django]-Python / Django | How to store sent emails?
Source:stackexchange.com