Just so you know, many first-time coders struggle with this exercise. I had a zoom call over the weekend to help explain this to another learner and it helped them just to be able to talk it out with someone. I will try to duplicate that conversation by asking/answering my own questions to try to walk us through the understanding.
So let’s begin with: what is our goal here? What are we trying to do here with this code? Short answer: we want to create a (clean) list of android apps, right? But why do we even need to do that? We need to do it because what we start with is a list that has duplicate entries for some of the apps and before we do any kind of analysis on our list, we want to make sure that we have only one entry per app: NO DUPLICATES PLEASE! But if an app has duplicates, how do we decide which one to put in our
android_clean list? Well, we have decided that we only want to add the one that has the most reviews (ie the value stored in the
reviews_max dictionary we created in the previous step) because that’s the one we feel will be the most up to date. So how do we do that?
Well, that’s what the code inside the
if statement is for! That code will check (on each iteration of the
for loop) whether or not the row (ie
app) corresponds to the app with the most reviews. We know this is true by comparing the number of reviews (
n_reviews) to the corresponding value stored in
reviews_max for the app. If the value stored in there is equal to
n_reviews then we know it has the maximum number of reviews and thus it’s the one we want to add to our clean list (
However, we also want to check if the app has already been added to our
android_clean list because it’s possible that more than one duplicate in our original list has the same max number of reviews. If we didn’t do a check to see whether or not the app with
n_reviews == max number of reviews has already been added to our clean list, we could end up adding the app more than once to our clean list, each one having the max number of reviews. We do this check by keeping another list:
already_added. This list just keeps track of the apps we’ve already added so that we can check if it’s been added or not. If the app has max reviews but it’s already been added to
android_clean, we don’t want to add it again. Alternatively, if the app has max reviews but has not already been added, we do want to add it to
Please let me know if any of this isn’t clear and we can try something else to help you out.