Skip to content
Advertisement

Count the existence number of a key in a list of dictionary

I used gspread and pandas to convert my google sheet into a list of dictionaries. The example list is shown as follows: (It’s a very long list, so I only list a few lines)

JavaScript

I want to count the number of the existence of some keys, for example Mike and Jane in the Receiver field. I tried the following to count the total number of the rows, and I think I may use a similar code

JavaScript

I just don’t know how should I count the number of existence for some keys. I’m new to python and I searched anywhere online with no luck

Advertisement

Answer

you can use len() and a generator:

JavaScript

you can use len() and filter():

JavaScript

you can use sum() and map():

JavaScript

this counts all the Mikes, if you want the Janes too, you have to add it to the condition.

Advertisement