Hi! can someone help interpre below code for removal of duplicate data? I dont understand if statement,n_reviews is a float number, how can reviews_max[name] compare with it? I think the element in reviews_max is the name of each app. and i don’t know what elif caluse for …
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
I dont understand if statement,n_reviews is a float number, how can reviews_max[name] compare with it?
reviews_max
is a dictionary. Dictionaries store key: value
pairs in them.
The keys
in reviews_max
are the names of each map. And the value
associated to each key
is the number of reviews for the corresponding app (n_reviews
).
So, in the if
condition, you perform the following steps -
-
If a particular app’s name
is already stored as a key
in reviews_max
, and if the value
corresponding to that key
in the dictionary is less than n_reviews
, you
- Replace the
value
corresponding to that key
with n_reviews
.
-
Else, if that particular app’s name
is not stored in the dictionary, you
- Create that
key: value
pair in reviews_max
If the above is still unclear, I would recommend going through the Missions where dictionaries are taught again. It should start making sense over time.
I found that piece of code quite complex too. See below how I broke it down.
If the name is not in review_max dictionary, create a new name. If it is in review_max dictionary (the Else statement), check if n_reviews is greater than the one already stored in review_max dictionary (the If statement inside the Else statement). If it is, then change the old one for the new one.
if name not in review_max:
review_max[name] = n_reviews
else:
if n_reviews > review_max[name]:
review_max[name] = n_reviews