# Can someone explain to me the logic in here?

In the first guided project after learning the fundamentals, I reach a stage where I need to create a dictionary where I will have unique apps and the most recent ones based on the no. of reviews they have (the more reviews, the more recent is the specific entry for a duplicate app).

``````    reviews_max= {}

for row in android:
name= row[0]
n_reviews= float(row[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
``````

What I fail to understand are the following:

• how is Python calculating` if name in reviews_max and reviews_max[name] < n_reviews:`. I fail to understand the logic here `review_max[name] < n_reviews`. This returns the app with the highest number of reviews. But a comparison between the key of the dictionary `reviews_max[name]` and no. of reviews `n_reviews` can give me the highest no of reviews.

• another aspect I do not understand why in the `elif` statement I’m appending again the `review_max[name] = n_reviews`.

I would be so glad if someone could help me with explaining this. Thank you so much

Initially `reviews_max= {}` is an empty dictionary. So `name in reviews_max` will return false since we are using `AND` operator, the whole if clause would return false.

Then it will execute the `elif` statement and since it is True, `n_reviews` gets stored in reviews_max[name]

(Note : `dictionary_name [key] = value` This is what happening here. So when you say `review_max[name]` is is returning the value, which is the number of reviews. So when you do `review_max[name] < n_reviews` this comparison, you are comparing the nr of reviews already stored in the dictionary and the new review that you’re getting from iteration.)

Now in the next iteration, if the name is already in the `reviews_max` dictionary, we need to check if the rating associated with the new name is higher than the previous one. `reviews_max[name] < n_reviews` will do that. If both of them are satisfied, the higher number of review gets assigned to the already existing dictionary key.

Initially `reviews_max= {}` is an empty dictionary. So `name in reviews_max` will return false since we are using `AND` operator, the whole if clause would return false.

Yes, `reviews_max={}` is empty in the beginning. What you said that `(if) name in reviews_max` will return false EVEN if we use the `AND` operator.

Then this will naturally run the `elif` clause. This results in that the first clause in the loop (`if name in reviews_max and reviews_max[name] < n_reviews`) will be `False` the first time, then it will run the `elif` clause which will append the `name` to the dictionary as a key. After this, the loop will go again and find the key `name` in the dictionary `n_reviews`together with using the `AND` operator.

So basically, the loop will find the name of the app together with the reviews and it will make that comparisons `reviews_max[name] < n_reviews`. What I fail to understand here is this (it follows an abstract example of the `if` statement in the loop):

if `App_1` (`name`) exists in the dictionary (`reviews_max`) and its review (`reviews_max`[name]) is less than the number of reviews (`n_reviews`) => the number of review for that app will be added to the dictionary.

• Here I fail to understand how actually Python selects the highest number of reviews for that specific app.

BUT

if `App_1` does not exist in the dictionary => then the number of reviews for `App_1` will be appended to its corresponding name (key)

• Here I understand that the app with its reviews will be added to the dictionary no matter what.

Sorry for the long post, but I would very much like to understand this.

@vallentin.ciocirlan

So you understand the first iteration. The `if` condition becomes false and `elif` condition gets executed and the key and value is stored in the dictionary.

In the next iteration, we are going to focus on 2 possible cases.

• the same app name is found with a higher number of reviews
• the same app name is found with a lesser number of reviews.

First case is something that we want to happen, since we are looking for apps with highest rating. So if first case happnes, we want to update our key, value pair.

So that happens in these lines.

Now consider the second case. We found an app name but its reviews are less than value stored at `n_reviews`. So we don’t want to update our dictionary with this value. If in the next line of code we used `else` instead of `elif`, this lower value would go and update the key, value pair in dictionary which we dont want.

In order to avoid that we are using

Here we are making sure that the value assignment happens only to those app names that hasn’t appeared yet in our dictionary. So we don’t update the dictioary with any lesser ratings.

I’m not sure if my explanation was simple enough, but I hope you got a bit more clarification. Please let me know.