Code Explanation

Hi Community,

i’m working on the Dictionaries And Frequency Tables Part on DQ.
I have a question to the code i don’t understand.
Mission 7. Counting with Dictionaries:

content_ratings = {‘4+’: 0, ‘9+’: 0, ‘12+’: 0, ‘17+’: 0}

for row in apps_data[1:]:
c_rating = row[10]
if c_rating in content_ratings:
content_ratings[c_rating] += 1


My question is here:
How is it possible to count with this statement? Does content_ratings[c_rating] += 1 referes to the value in the dictonary above? Maybe somebody can explain me this Code step by step?


Best regards

I will try my best to explain to you.

How is it possible to count with this statement?
The expression


is the counter.

it would be something equivalente to


The code means this:
It will iterate all the column 10 rows ( called c_rating) of apps_data and try to match this with content_ratings( 4+,9+,12+,17+) . if c_rating matches the content_rating it will count 1 ( to that specific c_rating) with the code "content_ratings[c_ratings]+=1.

Hope I could help you



Hi Carolina,

thanks for your explanation.
I understood it :).

Unfortunatly, when i look at the next Screen 8. Finding the Unique Values i’m also a little bit confused. Maybe you can give me some input and explain this to me, too:

The logic is the same like in my question before.
But when i look at the output i have a big questionmark.
The output should be only 1 because we don’t have any keys:values in the dictonary content_ratings. What is the statement counting?
Can you explain me the logic/the code on the screenshot?



This code is a counter that will count the number of times each unique string appeared in ratings and create a dictionary called content_ratings.

let’s emulate what is happening:

for c_ratings in ratings:

means it will iterate each item of ratings. So the first item is a “4+”

if c_ratings in content_ratings:

means that if there is a ‘4+’ in content_ratings it will add 1 to the counter

else: content_ratings[c_ratings]=1

otherwise it will create a ‘4+’ item in the dictionary.

After iterate all the steps the next item of ratings will pass through the same steps.

1 Like

Thanks for this!
Now I can go on :slight_smile:

@CarolinaAyumiTachiba if c_rating in content_ratings:
This statement will check whether c_rating value is present in content_rating or not.
My doubt is content_rating is empty dictionary it has no key inside it. So each time it will go in else condition??? So how actually this if condition is working

Hi @dhruvbhatnagar9548 and welcome to the community!

Yes, you are correct in that the dictionary starts off with no keys and no values…it begins its life as an empty dictionary. So it is true that in the beginning, the else statement will be executed a lot more. However, as we loop over content_ratings, we are feeding our if statement with more and more keys, some of which will be repeats. It is when we see the same key again that the if part will execute.

Thank you sir for clearing my doubt.

1 Like