Why i am getting key error?

`#function to select entries and print only entries with max no. of reviews

def reviews_max(dataset):
empty_dictionary={}
for row in dataset[1:]:
name=row[0]
n_reviews=float(row[3])

    if empty_dictionary[name] in empty_dictionary:
        if empty_dictionary[name]<n_reviews:
            empty_dictionary[name]=n_reviews
            
    elif empty_dictionary[name] not in empty_dictionary:
        empty_dictionary[name]=n_reviews
return empty_dictionary

print(reviews_max(dataset))
`


KeyError Traceback (most recent call last)
in
14 return empty_dictionary
15
—> 16 print(reviews_max(dataset))
17

in reviews_max(dataset)
6 n_reviews=float(row[3])
7
----> 8 if empty_dictionary[name] in empty_dictionary:
9 if empty_dictionary[name]<n_reviews:
10 empty_dictionary[name]=n_reviews

KeyError: ‘Photo Editor & Candy Camera & Grid & ScrapBook’

Hi there!

If you look at the error message, it tells you which line triggers the error:

the empty_dictionary[name] part of your code tries to retrieve the value with a key of name from the dictionary, but because in some cases that key is not available, you get that KeyError.

Instead, you can use the code if name in empty_dictionary which checks whether the key exists, which I’m almost certain is the logic you’re intending. With this code, it wont attempt to search for that key during your if statement.

I hope this helps to explain what is happening and why - please let me know if anything isn’t clear and I would be more than happy to clarify.