Home > Net >  How do I count duplicate words in this list of dictionary
How do I count duplicate words in this list of dictionary

Time:12-31

0

I have this List that is contained with multiple dictionaries.

[{"hex": "8a194ad32c2ffff"}, {"hex": "8a194ad32957fff"}, {"hex": "8a194ad32967fff"}, {"hex": "8a194ad32c2ffff"}]

How do count the duplicates in this list?

So that the output is as follows:

8a194ad32c2ffff : 2 
8a194ad32957fff : 1 
8a194ad32967fff : 1

Our even a similar output, I just need to be able to count the duplicates.

Thank you

CodePudding user response:

The most straightforward way:

l = [{"hex": "8a194ad32c2ffff"}, {"hex": "8a194ad32957fff"}, {"hex": "8a194ad32967fff"}, {"hex": "8a194ad32c2ffff"}]

count_d = {el["hex"]: 0 for el in l}

for el in l:
    count_d[el["hex"]]  = 1

print(count_d)

CodePudding user response:

You can use Counter from collections

from collections import Counter
count = Counter([ll['hex'] for  ll in l ])
print(count) 
  • Related