Explaining the unique_apps list & duplicate apps list in guided project

Screen Link:

Can someone explain in detail the code for looping through the unique_apps, duplicate_apps list? The ‘if’ statement is getting me confused since how does the for loop notice the unique_apps list? The list isn’t called in the first 2 lines of the for loop? How does the code recognize to add the names into the unique_apps list if we did not initially make it append names to the unique_apps list?

i don’t understand the logic in the code. What i know, is that the for loop will go through each row in the dataset, assign the 1st column (column “name”) to a variable named “name”. Problem is the “if name in unique_apps” line; does Python already add those names in the unique_apps list after each iteration in the for loop? Is that just how this for loop works? Because i cant see within the code, how Python knows to add those names to the unique list each iteration. I expect another line of code between the name = app[0] line & the if line, where we tell python to add the app names to the unique list, THEN call the if function.

Initial, blank list, after first iteration, add name, 2nd, add name, x iteration, if name was already in unique_apps, amend to duplicate list.

Hi TravisBob

Welcome to the community!

unique_apps and duplicate_apps are defined outside the for loop, that to before starting the for loop. Hence, these lists are accessible to both the for loop and the if condition.

At the very beginning, both lists are empty. The if condition will check whether 'Photo Editor & Candy Camera & Grid & ScrapBook' is in unique_apps; since it’s not there, it’ll be added to unique_apps list.
So, each of the app names will be added to the unique_apps list. Consider one of the duplicate apps, Quick PDF Scanner + OCR FREE; when we come across it for the first time it gets appended to the unique_apps list, next time when we come across it for the second time, it will be appended to duplicate_apps list.

So, yes the names are added to the unique_apps list.


This is the code where the app names are added to unique_apps list.

Hope its clear now

No, it doesn’t already know the unique apps, but that’s what the for loop is there to do - an easier way to understand it might be to switch the if statement around to:

if name not in unique_apps:

This might explain it a bit more easily, let’s assume that unique_apps is empty. The name variable equals “DataQuest App”. If “DataQuestApp” is not in unique_apps list, let’s add it to the list. In this case, the list is empty, so we can add it. Now, let’s iterate again: this time, we have a duplication “DataQuestApp” is there again! So we immediately go to the else part of the if statement: it’s a duplicate app because the name already exists in the unique_apps list, so we append it to duplicate.

Sorry if my explanation is a bit confusing, I’m learning too!

1 Like

yea, this is more clearer syntax to understand. Thanks