Finding the Unique Values Question

I’m on this section: https://app.dataquest.io/m/314/dictionaries-and-frequency-tables/8/finding-the-unique-values

And I saw this example but don’t really understand the purpose of it.

https://s3.amazonaws.com/dq-content/314/py1m4_cb19.svg

If the 12+ exists already why do we need a +1 in this case? I understand we’re counting it in this case but why is the “else” needed here? Won’t the original code do the same thing without the else.

also here is my code for this problem:

content_ratings =

for row in apps_data[1:]:
c_rating in row[10]
if c_rating in content_ratings:
content_ratings[c_rating] += 1
else:
content_ratings[c_rating] = 1
print(content_ratings)

how come i’m getting the error: TypeError: list indices must be integers, not str

comparing my code to the answer i can see the difference but can somebody explain this to me further?

for one why is content_ratings = {} instead of
and another c_rating = row[10] instead of in

Lists and dictionaries are different structures in Python and we call elements within them differently. With lists, you can have the same item repeated, and it’s distinct from the other items because of its position (index) in the list. With dictionaries, each item only exists once as a key, and the position in the dictionary is irrelevant.

In the example from the lesson, ratings = ['4+', '4+', '4+', '9+', '9+', '12+', '17+'] is a list that shows each individual item in a specific order. The dictionary version of this is content_ratings = {'12+': 1, '17+': 1, '4+': 3, '9+': 2}. The order of the data is lost, but it tells us how many of each rating there were in the list.

To get an item from a list, we use list_name[integer], which tells us the value in a particular position in the list. To get an value from a dictionary, we use dictionary_name[key], which tells us the value associated with the specific key. ratings['4+'] doesn’t make sense, because you can’t call the index (position) with the string. Likewise, content_ratings[1] doesn’t make sense, because there is no key 1 in the dictionary.

So looking at your code, you have content_ratings as a list, which means we can only put integers in the brackets. Since all the c_rating values are strings, you get the TypeError.

Finally the part with c_rating in row[10]. We’re using c_rating as a variable for whatever is in the 10th index of the row, and then later comparing to see if c_rating is one of the keys in the dictionary we’re creating. We need to use = to assign a value to c_rating.

I hope this helps you understand the code a little better. For the workings of the loop and the if/else statements, check out this post where another member had a similar question.

Let us know if you still need more clarification.

1 Like

thanks april. that explains a lot. i tink this definition part confused me a little but i understand it much more after your explanation. as all things, i just need to keep looking at it and practicing.

i have a question, if i want to learn sql while in the process of this lesson would that be ill-advised? i’m trying to familiarize myself with SQL so that I can put it on my resume pretaining to jobs that might find that more useful.

I’m glad it helped!

It’s up to you if you want to add in SQL, I understand why you’d want to do that to meet your goals. SQL and Python are pretty different. If you find you’re not absorbing the material then you might want to just focus on one of them for a little while until your comfort level goes up. You can try out this SQL tutorial on W3Schools to get a feel for the syntax.