Smack Alpha
Smack Alpha

Reputation: 1980

Count the occurance of same value for the key in dictionary python

I have a multiple dicts in the list. I want to count the number of occurances of the certain value from the list of dicts.

Here's the list of dict:

a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

In here, i want to count the occurance of Nill in the Key. How to make it possible.

Here's the code i tried:

from collections import Counter
a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]
s = 0
for i in a:
    d = (a[s])
    #print(d)
    q = 0
    for z in d:
        print(z)
        z1=d[z]
        #print(z)
        if z1 == "Nill":
            q = q+1
            co = {z:q}
    print(co)

Expected Output:

The count of Nill values in the list of dict

{a:0,b:1,c:0,d:3}

Upvotes: 0

Views: 1636

Answers (5)

Mark
Mark

Reputation: 92440

You can use the Counter directly by counting the boolean expression with something like this that takes advantage of the fact the the counter will count True as 1.

a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

c = Counter()
for d in a:
   c.update({k: v == 'Nill' for k, v in d.items()})

# c => Counter({'a': 0, 'b': 1, 'c': 0, 'd': 3})

Upvotes: 2

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11073

Try this:

from collections import defaultdict

c = defaultdict(int, {i:0 for i in a[0].keys()})
for i in a: 
    for k,v in i.items():
        if v=='Nill': 
            c[k] += 1 

dict(c) will be your desired output.

{'a': 0, 'b': 1, 'c': 0, 'd': 3}

Upvotes: 1

ErikXIII
ErikXIII

Reputation: 557

Like this?

a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

result = {}
for sub_list in a: # loop through the list
    for key, val in sub_list.items(): # loop through the dictionary
        result[key] = result.get(key, 0) # if key not in dictionary, add it
        if val == 'Nill': # if finding 'Nill', increment that value
            result[key] += 1

for key, val in result.items(): # show result
    print(key, val)

Upvotes: 1

naivepredictor
naivepredictor

Reputation: 898

EDIT:

To match the output required:

import pandas as pd
df = pd.DataFrame(a)
occ = {k: list(v.values()).count('Nill') for k,v in df.to_dict().items()}

Upvotes: 1

Udit Hari Vashisht
Udit Hari Vashisht

Reputation: 549

Try this :-


a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

result_dict = {'a' : 0, 'b' : 0,'c' :0, 'd' : 0}
for i in a:
    for key, value in i.items():
        if value =="Nill":
            result_dict[key] +=1

print(result_dict)

Upvotes: 2

Related Questions