Step 1 - 11) Keeping dictionaries separate (doubt)

Hi i have shared two links. I am confused as to why the code used ‘key’ in for loop while in the second link the code used ‘rating’ in for loop. Also i didn’t understood the second and third line of the code. I am new here so posting my doubt for the first time, this post got withdrawn by author yesterday and i don’t know why. Any suggestions are also welcomed.

Screen Link 1:
Screen Link 2:

My Code:

for key in content_ratings:
    proportion = content_ratings[key] / total_number_of_apps
    c_ratings_proportions[key] = proportion

What I expected to happen: why the code used ‘key’ in for loop

Hi @uditchauhan: key is used to obtain its corresponding value which is used to do the arithmetic as described in this article.

1 Like

Sir , then why “rating” is used in for loop in the second link mentioned by me in the question. Thank you for your help :slight_smile:

Hi @uditchauhan:

Actually its up to you whatever you want to name the iterator, as long as you are consistent. For the second link my code is as follows:

I used i instead of rating and it is still correct. They use rating mainly for readbility sake.

content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}
total_number_of_apps = 7197
for i in content_ratings:
    content_ratings[i] /= total_number_of_apps
    content_ratings[i] *= 100
percentage_17_plus = content_ratings['17+']
percentage_15_allowed = content_ratings['4+']+ content_ratings['9+'] + content_ratings['12+'] 

Hope this clarifies.

2 Likes thank you, it cleared my doubt. But does it mean that in the first link also i can use anything instead of key? And what does the calculation in the second and third line means ?

Yep @uditchauhan . Just need to be consistent throughout.

1 Like i also had difficulty in understanding the second and third line of the code i mentioned

Hi @uditchauhan: The second line is basically storing the proportion of ratings in a variable proportion, which is a float between 0 and 1. Since you wish to append the value of content_ratings dictionary to the c_ratings_proportions dictionary, the third step is important because it specifies the key in which the “value”, which is the calculated proportion (i.e. proportion) is stored. So you will get:

c_ratings_proportions =
{'12+': 0.16048353480616923,
 '17+': 0.08642489926358204,
 '4+': 0.6159510907322495,
 '9+': 0.13714047519799916}

The same logic applies for the percentages.

1 Like thank you sir.