Guided Project 1 - Slide 12

I have seen the answer. However, I wanted to try to do it differently to improve my understanding. This concerns slide 12 of guided project 1 (basic python).

The purpose is to present average of each genres. Can someone please help to explain what I am doing wrong:


def avg_rating(dataset, index):
tables = {}
len_genre = 0

for row in dataset:
    key = row[index]        
    if key in tables: # e.g if Social Networking is the prime_genre
        tables[key] += float(row[5]) # will add rating_count_total to table
        len_genre += 1
    else:
        tables[key] = float(row[5])
        len_genre = 1

#pretty much similar to slide 11 when the same code produces genres as % of the total apps. However, this code does not work to show average for all genres. Can someone please help me to understand what I am doing wrong. Thanks.

avg_n_ratings = {}
if key in tables:
    average = tables[key] / total
    avg_n_ratings[key] = average 

return avg_n_ratings

avg_rating(ios, -5)

Output:
{‘Food & Drink’: 239.14297385620915}

I am not getting the complete dictionary showing each genre with the average.