Can someone explain me what's happening in the if statement

reviews_max = {}
for apps in google_apps_data[1:]:
    name = apps[0]
    n_reviews = float(apps[3])
    if name in reviews_max and reviews_max[name] < n_reviews: # i am facing difficulty in this line. reviews_max[name] is a string and n_reviews is a float.
        reviews_max[name] = n_reviews
    elif name not in reviews_max:
        reviews_max[name] = n_reviews

Hi prudhvichowdary14,

Could you wrap you code (on newlines) with triple back ticks like ``` ? That allows the code to be formatted as a code block with proper indentation.

if is codeblock:
    print('this is example codeblock')

It’s very difficult for someone to read the code pasted in current state

1 Like
reviews_max = {}
for apps in google_apps_data[1:]:
    name = apps[0]
    n_reviews = float(apps[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

Can you please edit the question instead of adding a reply?

Can you also specify what is it about the code that you don’t understand? Depending on the required detail, there could be a lot to cover, or simply a one-answer sentence.

I edited the question. Please look once and help me in understanding the code.

I am not sure if I know your question exactly…but if you want to turn the string to float, you may try the following line:

 if name in reviews_max and float(reviews_max[name]) < n_reviews:

I hope I have interpreted your question correctly. The part that says reviews_max[name] < n_reviews is trying to see if the float value assigned to the key [name] is less than the float value for n_reviews.

For example, say you had the following already in the dictionary reviews_max:
{'Games' : 34, 'Productivity' : 56, 'Social media' : 29}
This means:
reviews_max['Games'] = 34
reviews_max['Productivity'] = 56
and reviews_max['Social media'] = 29

So in the next iteration of the loop, if name = 'Games' and n_reviews = 78, then it’s really checking if 34 < 78 since reviews_max['Games'] = 34.

I hope that helps.

Thank you so much for helping me out.