Confused re: Looping through apps_data

Screen Link: Learn data science with Python and R projects

My Code:

for row in apps_data[1:]:
    c_rating = row[10]
    if c_rating in content_ratings:
        content_ratings[row] += 1

The correct code:

for row in apps_data[1:]:
c_rating = row[10]
if c_rating in content_ratings:
content_ratings[c_rating] += 1


this may be a silly question but I am still early in my lesson re: Python dictionaries.
I don’t understand this problem: " * Check if c_rating exists as a key in content_ratings. If it exists, then increment the dictionary value at that key by 1 (the key is equivalent to the value stored in c_rating)."
.
Why is the dictionary value code: content_ratings[c_rating] ← why is c_rating in the brackets?

Not a silly question. Never worry about asking a question.

I would first recommend going through this Screen again. Don’t hesitate to review any of the previous content if something seems confusing. Feel free to ask more questions if the previous content doesn’t help clarify it for you.

1 Like

Hi @milkskinlove

I hope the above reply helps you. If you want a bit more clarity, try printing row and c_rating inside the loop and you’ll soon figure out the answer.

1 Like

I sort of understand. I understand the for loop but when it’s time to insert indices 'I am getting confused." I am moving on and hopefully it clicks.

Hi @milkskinlove

You should be moving on after understanding about the dictionaries and how value is stored in dictionary using the keys.

dictionary_name[key_name] = value_of_value stores value_of_value in the dictionary using the key_name

For eg dict[first] = 1 stores 1 in the dictionary using the key first. In order to access the value, or to reassing it, you can use dictionary_name[key_name]

if you say dict[first] +=1 it will then store 1+1=2

Now if you look at the question you asked, row represents a row in apps_data in each iteration, while c_rating represents the rating value in each of the rows of apps_data.

Now we want the content rating to be the key for our dictionary, not the whole row. That is why we are using c_rating instead of row. I hope this clarifies your question.

1 Like

Hello @jithins123 ,

Yes, yes, yes! This surely clarifies my question. I’m sorry I do require ‘a little hand-holding’ i suppose and i don’t want to get de-motivated.

thank you so much for this kind of reply; helps so much more than just re-reading a lesson.