350.5 Dictionary building question

Solution notebook for the guided project Profitable App Profiles for the App Store and Google Play Markets.

The area where I have a question is at part two where the dictionary is built.

Code in Question

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

I am assuming that reviews_max[name] value is a string value containing the index position 0 value which is the name of the app. Why does the author compare name with the number of values??? Appreciate any guidance!!! Thank you!

Full Code

reviews_max = {}

for app in android:
    name = app[0]
    n_reviews = float(app[3])
    if name in reviews_max and reviews_max[name] < n_reviews:
        reviews_max[name] = n_reviews
    elif name not in reviews_max:
        reviews_max[name] = n_reviews

Hi @genesix32, welcome to the community! Thank you for framing your question so well!

In short, reviews_max[name] is a float value, the number of reviews given to the app name in the dictionary. In a dictionary, dictionary_name[key] = value, so the author is not comparing the name, but the value associated with the name.

Just to be thorough, let’s walk through an example.

Let’s say our dictionary so far looks like this:

{'App1': 35, 'App2': 12, 'App3': 9}

In the next iteration of our loop, let’s say that name = 'App4' and n_reviews = 17. The if-statement will check if App4 is in the dictionary. It isn’t, so it will process the elif and add reviews_max['App4'] = 17 to the dictionary.

Now our dictionary looks like this:

{'App1': 35, 'App2': 12, 'App3': 9, 'App4': 17}

Let’s say that on the next iteration, name = 'App2' and n_reviews = 23. This time App2 is already in our dictionary, so we check if reviews_max['App2'] < 23. Since reviews_max['App2'] = 12, then we need to update our dictionary so that reviews_max['App2'] = 23.

Now our dictionary looks like this:

{'App1': 35, 'App2': 23, 'App3': 9, 'App4': 17}

I hope that helps. Let us know if you’re still having trouble.


Lets also assume that name=“App2” and n_reviews =8 now we know that the other part of the condition is not satisfied which eventually makes that statement evaluate to false. I want to know what happens then since the elif statement didnt carter for that part.

In that case, the dictionary remains unchanged, and then it moves on to the next iteration of the loop.

That’s why we use elif instead of else in our conditional statement. If we had written else:, then it would automatically change the value of reviews_max[name] to n_reviews. The elif ensures that it has to meet the conditions of either not already being in the dictionary, or having a higher value than what’s already in the dictionary. If it fails either of those conditions, we don’t want the dictionary to change at all.

1 Like

Hats off

I’m grateful.
Thank you.

1 Like

I’m here again
free_googleApps =
free_appleApps =

for app in google_englishApps:
price = app[7]
if price == ‘0’:

for this_app in apple_englishApps:
price = app[4]
if price == ‘0.0’:

If I run this code, in a bid to obtain the length of each dataset, I’m getting 0 for apple apps. I observed that it was because it to “0”. Please, I am a bit confused why?

You’re using this_app as the iteration variable but referencing app[4]. :wink:

I’m bit confused with this code:

reviews_max = {}

for app in android:
name = app[0]
n_reviews = float(app[3])

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

In the code above, What happens if name in reviews_max and reviews_max[name] = n_reviews?

If the reviews_max dictionary holds the maximum reviews for each unique app, why do we have duplicate apps with the same maximum review in the dictionary?

Also why does the android_clean list length same with the unique apps length, despite removing the duplicate rows?

1 Like

But I do not understand one thing, why we do not use else but else if?

Hi @aidacurraj1802 welcome to the community.

Please check this reply and see if clarifies your doubt. This is a reply for a different topic but applies here

If this doesn’t clarify your doubt, please feel free to ask again. In case you want to create a new topic please follow this guideline

I still do not understand particularly in my case. The explanation is that if we use else than our dictionary values will be updated incorrectly. why, and how, cannot you create a very simple dictionary and explain it? Because in theory I say yes I know the difference between elif and else, but in this case I do not understand what would happen?

Hi @aidacurraj1802

The above code says

if the name of the app in the current iteration is in the review_max dictionary AND if its corresponding value in the dictionary is less than the value of n_reviews, we need to assign the higher value to the dictionary key, because we are looking for the highest rating for each app name.

If we use else clause, if any one of the condition fails it will skip the if code block and run the code block under else clause.

reviews_max[name] = n_reviews

If the app name is NOT present in the dictionary, the else clause assigns n_review to reviews_max[name]'. its all good in this case because We want that to assign value to new app names. In this case, using of else` clause is fine.

BUT if app name is present in the dictionary but if the rating value stored is higher than n_rating the if clause will return False due to True AND False condition. In that case if there was only an else clause and no condition, the else clause will run reviews_max[name] = n_reviews and stores a lower value in the dictionary. (because that is what you’re telling the code to do)

In order to avoid(avoid storing lower ratings) that, we dont want the last code block to run by default if the first condition fails. We want to make sure that name not in reviews_max when the if condition fails. So we know that it is not storing any lower ratings when the first condition fails. Hence we are using elif.

Hope it is clear now.

perfect reply thank you very much

1 Like