# How to count the number of occurrences of dictionary keys

**URL:** <https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383>\
**Category:** Python Help\
**Tags:** help\
**Created:** [January 28, 2022, 12:24am UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383 "2022-01-28T00:24:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aidenfaustine](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aidenfaustine/32/6097_2.png) [@Aidenfaustine](https://discuss.python.org/u/Aidenfaustine)\
**Post date:** [January 28, 2022, 12:24am UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/1 "2022-01-28T00:24:31Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/6/6a454c10dea49beaa5bb626af181dd25d556f84b.png)  
I am new to python. How to count the number of occurrences of dictionary keys?

I just want to know different numbers aboout emotion. The data is attached  
thanks  
best wishes

---

<div class="post-metadata">

**Author:** ![Aidenfaustine](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aidenfaustine/32/6097_2.png) [@Aidenfaustine](https://discuss.python.org/u/Aidenfaustine)\
**Post date:** [January 28, 2022, 12:27am UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/2 "2022-01-28T00:27:03Z")

</div>

Just want to count the number of emotions corresponding to different emotions, such as how many times sadness appears in the dev dataset? Excuse me, thank you

---

<div class="post-metadata">

**Author:** ![TobiasHT](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tobiasht/32/23304_2.png) [@TobiasHT](https://discuss.python.org/u/TobiasHT)\
**Post date:** [January 28, 2022, 8:07am UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/3 "2022-01-28T08:07:01Z")

</div>

Hey Aiden,

Is there anything you had written before so we can start from there?

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [January 28, 2022, 10:26am UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/4 "2022-01-28T10:26:32Z")

</div>

Start by describing how **you** would count the number of occurrences,  
using pencil and paper. What would you do?

Now think about how you would explain it to a five year old. What would  
you tell them to do? Write out your instructions in the most clear way  
that you can.

Imagine that you are writing instructions for this guy:

[![](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/d/d3c7159e032cc4383130e3413ee587648ed074a2.jpeg "THIS "EXACT INSTRUCTIONS CHALLENGE" IS SO HILARIOUS") ](https://www.youtube.com/watch?v=Ct-lOOUqmyY)

That is what programming is like.

But it always starts with working out how _you_ would solve the problem  
by hand, then writing some code. So how would you solve the problem?

---

<div class="post-metadata">

**Author:** ![Aidenfaustine](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aidenfaustine/32/6097_2.png) [@Aidenfaustine](https://discuss.python.org/u/Aidenfaustine)\
**Post date:** [January 28, 2022, 2:32pm UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/5 "2022-01-28T14:32:58Z")

</div>

Dear TobiasHt:

Thank you for your reply.  
I only know how to print the value corresponding to a specific key.  
But when I count the dev dataset, I don’t need to care about which conversation it comes from, so the second key value can take a random value. I only care about the number of emotion statistics corresponding to emotion in the dev dataset. Could you please help me to fix this issue?  
Thanks. This is attached the original data  
[https://raw.githubusercontent.com/declare-lab/MELD/master/data/MELD/datasets.yaml](https://raw.githubusercontent.com/declare-lab/MELD/master/data/MELD/datasets.yaml)

config[‘dev’][‘random value’][‘Emotion’]

config[‘dev’][‘dia0\_utt0’][‘Emotion’]

import yaml

from collections import Counter

config = yaml.load(open(’…/content/drive/MyDrive/mfcc\_preprocess/melddatasets.yaml’, ‘r’, encoding=‘utf-8’).read())

emotion = config[‘dev’][‘dia0\_utt0’][‘Emotion’]

print(emotion)

---

<div class="post-metadata">

**Author:** ![TobiasHT](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tobiasht/32/23304_2.png) [@TobiasHT](https://discuss.python.org/u/TobiasHT)\
**Post date:** [January 28, 2022, 7:27pm UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/6 "2022-01-28T19:27:06Z")

</div>

I feel like I’ve not understood what your’e asking. However, there are two ways of solving this problem. You either have to explain your issue until someone understands it and helps you write the code,  
OR, you can take time and take a simple python course to learn the basics which you’ll apply in solving this problem. It might be a book, crash course or YouTube tutorial, whatever you feel comfortable with.

Personally I’d go with the second option cause then you’ll grow your skills as a developer.

---

<div class="post-metadata">

**Author:** ![Aidenfaustine](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aidenfaustine/32/6097_2.png) [@Aidenfaustine](https://discuss.python.org/u/Aidenfaustine)\
**Post date:** [January 28, 2022, 8:40pm UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/7 "2022-01-28T20:40:03Z")

</div>

Dear TobiasHT:

I am very grateful for your help and suggestions. Last hour, I successed in fixing this issue.

Thanks  
best wishes  
aiden

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [January 29, 2022, 6:52am UTC](https://discuss.python.org/t/how-to-count-the-number-of-occurrences-of-dictionary-keys/13383/8 "2022-01-29T06:52:05Z")

</div>

One way to count ‘Emotions’ (assuming that you have pyyaml installed):

```python
import yaml
from collections import Counter

with open('datasets.yaml', 'r') as f:
    data = yaml.safe_load(f)

count = Counter(nested['Emotion'] for record in data.values() for nested in record.values())

```

On dataset provided `count` will be:

```auto
Counter({'neutral': 6435, 'joy': 2308, 'surprise': 1636, 'anger': 1607, 'sadness': 1002, 'disgust': 361, 'fear': 358})

```
