Guided Project: Profitable App Project - Review Dictionary

I am completing my first guided project. I found the code that makes the dictionary of maximum review quite complex. See below:

reviews_max = {}
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 think this piece of code does the same thing and is simpler to read:

goo_dict = {}
goo_apps = goo[1:]

for app in goo_apps:
name = app[0]
nrev = app[3]
if name not in goo_dict:
goo_dict[name] = nrev
else:
if nrev > goo_dict[name]:
goo_dict[name] = nrev

You Are Totally Correct!!

BUT, (and this is a big but :smile:) There are a couple of pointers to discuss :

First: Indentation Matters
I’m sure you know this, just confirming:
Spaces or tabs at the beginning of a statement is a crucial part of Python syntax.
A missing or extra space in a Python block could cause an error or unexpected behavior.

So your code must look like this:

goo_dict = {}
goo_apps = goo[1:]

for app in goo_apps:
    name = app[0]
    nrev = app[3]
    if name not in goo_dict:
        goo_dict[name] = nrev
    else:
        if nrev > goo_dict[name]:
            goo_dict[name] = nrev


Second: you can use (elif) rather than (else: if)



Third: You forgot to convert the number of reviews from integer to float:

    nrev = float(app[3])


Also, It is worth noting that your code is 10 lines long vs the solution that is only 8!

1 Like

Thank you for pointing out the indentation. I had a hard time formatting.

According to the PEP and Zen of Python, shorter is not always better. Choose the simpler and readable.