Adding dictionary values for specific keys using if and else statements

Hi there,

I am here: Dictionaries and Frequency Tables

My question is whether something similar to this would make sense (way 2 in the screenshot below) or if it wouldn’t be possible? I am trying if an else statements inside a for loop to add dictionary values to keys that are not ‘17+’. Or is way 1 more straight - forward/ easier?

Thank you so much!

1 Like

Hi @thaisalemr,

In #way 2, you are checking if '17+' in content ratings: on each iteration, which will always evaluate to True as 17+ exists in content_ratings dictionary. Therefore, the else: block will not be executed and app_15_allowed dictionary will stay empty.

Hope this helps :slightly_smiling_face:.


1 Like

Hi @thaisalemr,
in #way 2 you can change the condition to if value == ‘17+’

apps_15_allowed = 0
for value in content_rationgs:
    if value == '17+':
        apps_17_plus = content_rationgs[value]
        apps_15_allowed += content_rationgs[value]
1 Like