Dictionary comprehension - Project 2

Solution: https://github.com/dataquestio/solutions/blob/master/Mission350Solutions.ipynb

Hi, finding this very difficult to wrap my head around.

reviews_max = {} # This creates an empty dictionary

for app in android:# Loops over the data
    name = app[0] # variable that is equal to an empty list
    n_reviews = float(app[3]) # A variable that is in row 3 which is ratings and changed to a float
    if name in reviews_max and reviews_max[name] < n_reviews: # Ok so if name in reviews_max is just checking if the name is
        # allready in there. But this? and reviews_max[name] < n_reviews - is this checking if the name of the app is less 
        #than the reviws?
        reviews_max[name] = n_reviews # lost. im assuming this needs to be false? in order it to go to the next line 
    elif name not in reviews_max: # So this is checking if names are not in reviewd max, im guessing it must be true
        reviews_max[name] = n_reviews # The name is eual to number of reviews if this statement is true
print('Expected length:', len(android) - 1181)
print('Actual length:', len(reviews_max))

The for loop I Understand. The code below is a bit more difficult:

    if name in reviews_max and reviews_max[name] < n_reviews:
        reviews_max[name] = n_reviews 

I can look at this logically and i know its checking over to see if the names are in the list but reviews_max[name] < n_reviews: is eluding me. I am close to see whats going on, but why is it checking if
its less than the number of reviews?

Then we get too the code below, I know its checking if something is not in the reviews max which would be true, then its assigning it to the variable we created before.

elif name not in reviews_max: 
        reviews_max[name] = n_reviews 

I think what throws me off is a mixture of the syntax and being able to match up things. I look at logically and
I see, ok there is a variable, there is a variable with a list, its less than the number of reviews. But comprehending as a whole is where it becomes very difficult for me.

Thanks for any help

Hi @Frankie ,

You are right. You are checking in your dictionary reviews_max to find out if the app name is in the dictionary reviews_max.

Remember it will only search through the keys (which are the app names in reviews_max dictionary.

reviews_max{app_name,n_ reviews}
The key is app_name and the value is n_reviews

Pretend this is your current reviews_max dictionary

reviews_max =  {"spotify": 400,  "spotify":800, "atari":9000, "atari":790}

There are apps with same name but different ratings amount. Your goal is to loop through the dataset and pull out the apps with the highest n_reviews. So, like in the pretend reviews_max dictionary above, you want to pull out “atari”:9000 since 9000 is more than 790.

Now look at your for loop again:

for app in android: 
    name = app[0]   # pulls out the app name and saves app name in name variable

    n_reviews = float(app[3]) # stores the number of reviews for the app in **n_reviews** variable
    # you will later compare this n_reviews with the current n_reviews value in your reviews_max  dictionary

    if name in reviews_max and  # if the app name is already in reviews_max dictionary

    reviews_max[name] < n_reviews:  # you are comparing the current value in the reviews_max dictionary to the above value from for loop n_reviews = float(app[3]).  

        reviews_max[name] = n_reviews  #If the new n_reviews is higher then assign new n_reviews to dictionary
    elif name not in reviews_max: # if name is not in the reviews_max dict then add the name with the n_reviews as the value
        reviews_max[name] = n_reviews

Hi Casandra.

Thank you for this. Yep that makes perfect sense. When it’s explained well it just clicks.

When I read what you wrote in the if statement, it all just clicked.

I’m sure in a few months reading things like this will be second nature. I got to remind myself that things won’t make sense all the time and that’s ok!

This community is so helpful, it really makes a huge difference.



So true! :100: This community really helps… :grinning:

You are really doing a great job just by continuing to learn and ask …