Can someone explain this code down for me?

reviews_max = {}

for app in android_data:
    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
        
print(reviews_max)

https://app.dataquest.io/m/350/guided-project%3A-profitable-app-profiles-for-the-app-store-and-google-play-markets/5/removing-duplicate-entries-part-two

I’m not understanding this at all.

name is the name of the app
n_reviews is the number of reviews for that app that turned into a number
If the app name is in reviews_max AND the name in the reviews_max is less than a number
Then the name of the app in the reviews_max is equal to the number of reviews.

How can a name be equal to a number? I’m assuming reviews_max[name] is looking for the name of the app in the reviews_max dictionary.

Click here to open the screen in a new tab.

I would suggest going through the Dictionary related material again in the content to understand what that code actually does. It should clear it up for you soon. If you are still stuck after that then you can ask further specific questions here.

@ shin.lee.310 Shin Lee 310

Let me give you an introduction of dictionary:

syntax of a dictionary: dict_name = {key1:value1, key2: value2, key3:value3,.........}
Now I am creating an example dictionary:
my_dictionary = {"Rina": 20, "Alice": 30, "Ellen": 25}
Let’s assume that Rina, Alice and Ellen are the names of my three friends and the associated numbers with each name is their ages respectively.
According to the dictionary syntax key1 is “Rina” and value1 is her age 20.
According to the dictionary syntax key1 is “Alice” and value1 is her age 30.
According to the dictionary syntax key1 is “Ellen” and value1 is her age 25.

How can you retrieve the values from a dictionary??

first_value = dict_name[key1]
print(first_value)
Output: value1

Example:

Rina_age = my_dictionary["Rina"]
print(Rina_age)
Output: 20

How can you retrieve all the values from a dictionary?

for key in dict_name:

   value = dict_name[key])
   print(value)

Example:

for name in my_dictionary:

   age = my_dictionary[name])
   print(age)

Output:
20
30
25

How can you add a new key_value pair in a dictionary?

dict_name[key4] = value4
Output:
dict_name = {key1:value1, key2: value2, key3:value3, key4:value4}

Example:

my_dictionary["Diana"] = 23
print(my_dictionary)
Output:
my_dictionary = {"Rina": 20, "Alice": 30, "Ellen": 25, "Diana": 23}

How to create a empty dictionary and fill it up with key_value pairs?

dict_name= {}
dict_name[key1] = value1
dict_name[key2] = value2
dict_name[key3] = value3
print (dict_name)
Output:
dict_name = {key1:value1, key2: value2, key3:value3}

Example:

my_dictionary= {}
my_dictionary["Rina"] = 20
my_dictionary["Alice"] = 30
my_dictionary["Ellen"] = 25
print (my_dictionary)
Output:
my_dictionary = {"Rina": 20, "Alice": 30, "Ellen": 25}

Now let’s come to your mission code:

Let make a little bit change to make it more readable:

Here we are making a dictionary with unique app names(keys) and the associated maximum number of reviews(values) to get rid of duplicate app names entry. We have same named apps with different number reviews again and again in our dataset.
We decided to keep the maximum reviewed entry for each app name.

  1. Create an empty dictionary:

    reviews_max = {}

  2. Iterate over your data set android_data and extract the names and number of reviews and assigned them to variables name and n_reviews respectively.

    for app in android_data:

       name = app[0]
       n_reviews = float(app[3])
    
  3. If the name is not in the dictionary already(That means it’s a new name) add it as key and assign the number of reviews to it as a value.(name : n_reviews (key:value)pair),
    if name not in reviews_max:

     reviews_max[name] = n_reviews
    
  4. Otherwise, if the name is already in the dictionary(that means loop is finding the name repeatedly and it has different number of reviews), so also check the current number of reviews of (which loop finds) is greater than the already assigned number of reviews to the name, if so, rewrite(update or change) it with the new founded number of reviews, as we said we need the maximum number of reviews.
    elif name in reviews_max and and n_reviews > reviews_max[name]:

     reviews_max[name] = n_reviews
    

FYI:reviews_max[name] doesn’t contain the name, in fact it contains the number of reviews associated to it.

  1. Print the dictionary:
    print(reviews_max)

I will be really glad if it can clear your confusion.
Best,
Raisa

4 Likes