# Unique contents rating in dictionary

I need an explanation on this codes below as I couldn’t understand it.

.I need an explanation please.

Dictionary in Python holds two things key and values against the key. The following code is counting the frequency of the ratings. That means counting how many times the same rating appears.
Look at the first line of the code. content_ratings is a dictionary with keywords 4+, 9+, 12+ and 17+ with all zero values.
The second line ratings contains some ratings which are the keywords of the dictionary defined earlier.
The loop iterate over the list elements ratings.
The conditional statement matches the key with list values and increasing the values against matched keywords. Thus, the frequency/ number of occurrences is counted.

The second which contains ratings has some of the keys repeated for example the 4+ is repeated 3 times and the 9+ twice. Then how do we come about these statement…
for c_rating in ratings:
if c_rating in content_rating:
content_rating [c_rating ] +=1.
I really don’t understand these codes.

I tried my best to make it realizable. If it is still not clear to you, I will suggest you recall your list and dictionary basics.
thank you

3 Likes

@amosdaramola956 Let us know if @Zubair’s wonderful (and beautifully calligraphed) explanation above isn’t sufficient!

I suspect you were struggling with either the `content_rating[c_rating] += 1` bit, or the `for c_rating in ratings` part.

If it’s the former, understand that `content_rating[c_rating] += 1` is simply `content_rating[c_rating ] = content_rating[c_rating ] + 1`. I.e. you’re adding 1 to its existing value.

If it’s the latter, there was a thread just recently on for loops that should shed some light on the basics of how they work.