Built in function question:

Could somebody please help me with these questions, I am really confused rifght now

ratings = ['4+', '4+', '4+', '9+', '12+', '12+', '17+', '17+']
content_ratings ={}
why dont why write content_rating =[]but content_ratings ={}
for rating in ratings:
    if rating in content_ratings:
        content_ratings[rating] +=1
    else:
        content_ratings[rating] =1
print(content_ratings)

why don’t we use content_ratings.append(rating) but content_ratings[rating].

I’m a C# developer and just started getting into Data Science. It’s been a while since I’ve worked with Python so I’ll try my best to answer your questions.

You create a dictionary using the {} brackets. You create a list with [] brackets. These are different types of data structures with different behaviors.

At high level, what this logic is doing is performing a count of ratings and aggregating their totals in the content_ratings dictionary. We want to use content_ratings[rating] to access the rating’s value and aggregate it. The append method is not available for dictionaries.

I hope this helps!

3 Likes