Counting with dictionaries 7/15

Hi, I am testing different types of code, but I not really understand why my code is not counting ratings in appstore table, column 10.

This is my code and it’s wrong:

content_ratings={‘4+’:0,‘9+’:0,‘12+’:0,‘17+’:0}
ratingss=apps_data[1:]

for c_rating in ratingss[10]:
if c_rating in content_ratings:
content_ratings[c_rating]+=1
print(c_rating)

But if only I type it works:
for ratingss in apps_data[1:]:
c_rating=ratingss[10]
if c_rating in content_ratings:
content_ratings[c_rating]+=1
print(content_ratings)

Why in 1st version of my code it doesn’t read “c_rating in ratingss[10]” if I have already named the variable ‘ratingss’ before?
But second code which has the same sense but without c_rating = ratingss[10] is working?
Can somebody explain why?

1 Like

Hi @klausuts welcome to the community!

Your ratingss variable from apps_data[1:] is going to be a list of lists. Each row of the CSV becomes a list. Because each element is a list, or row, ratingss[i] corresponds to one row of the dataset. So when you look at ratingss[10], you’re looking at a particular row, not a particular column.

print(ratingss[10])

output:

['512939461', 'Subway Surfers', '156038144', 'USD', '0.0', '706110', '97', '4.5', '4.0', '1.72.1', '9+', 'Games', '38', '5', '1', '1']

When you try to use for c_rating in ratingss[10], it’s going to take each element of the ratingss[10] list above and see if it exists as a key in the content_ratings dictionary. Once it’s finished looking at this one row, it stops because it wasn’t told to check any other rows of the dataset.

So now let’s look at the other code snippet where ratingss is used differently. When you write for ratingss in apps_data[1:]:, you go through each row of apps_data and call that row ratingss. Then you will take the 10th index of that row with ratingss[10] and assign it to the c_rating variable. Once the if-statment runs and it updates the dictionary counts, it will go through the next row on until the end of the dataset.

To summarize, the difference between the 2 pieces of code is that when ratingss was a list of lists, ratingss[10] meant the row at the 10th index. When ratingss was just a single row, then ratingss[10] meant the value from the column you wanted in that row.

I hope that helps. Let me know if you need more clarification.

2 Likes

@april.g Thank you a lot for such a detailed answer.
Your explanation is clear and understandable. Now I see the logic in this example.
Thank you once again and have a nice day! :slight_smile:

1 Like