Sum dictinary values

    content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}

total_number_of_apps = 7197
for rating in content_ratings:
content_ratings[rating] /= total_number_of_apps
content_ratings[rating] *= 100

percentage_17_plus = content_ratings[‘17+’]
percentage_15_allowed = content_ratings[‘4+’] + content_ratings[‘9+’] + content_ratings[‘12+’]

was the answer for looping through dictionaries. Is there a way to sum all the content ratings except ‘17+’ then multiply by 100 for percentage_15_allowed? something like
percentage_15_allowed = sum(content_ratings[rating][-1])

Hi @3fer

Welcome to our DataQuest Community

Yeah, you can do it in a line,

percentage_15_allowed = (sum([content_ratings[rating] for rating in content_ratings if rating != '17+']) / total_number_of_apps) * 100

What I am doing is, appending content_ratings[rating] by looping over all elements of content_ratings if rating is not equal to ‘17+’ and then summing up all the values of list.

This is called as List Comprehensions you will learn about it in Course 5: Data Cleaning In Python: Advanced.