Question on Iteration Code for This Section

This is perhaps a stupid question, but I am trying to parse why it is in this mission:

https://app.dataquest.io/m/314/dictionaries-and-frequency-tables/7/counting-with-dictionaries

We check content ratings and add it to age ratings, we have to:

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

As opposed to:

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

I know the second method will return an error when we try to run the code, and I understand the first method is what I need to do in order to achieve the desire result, I am just trying to determine why this needs to be done.

1 Like

This is more to do with how Python works internally.

If I give you a list, as an example -

a = [5, 19, 17, 30]

We know that we can access a particular value at a given index easily using something like -

``````a[2]
``````

The above will give us `17`.

We can also do something like -

``````for item in a:
print(item)
``````

Based on how Python works internally, that particular `for` loop, knows that what we want to do is check each value/element in that list `a`, and print out that value.

However, something like the following -

``````for item[2] in a:
print(item)
``````

will throw an error, just like you would have gotten in your code. In this case, Python does not know what `item[2]` is. `item[2]` does not exist as per Python. `a[2]` exists since `a` is defined and stored somewhere in memory. But there is no underlying implementation in Python for it to figure out what `item[2]` is.

I am being vague here because this is somewhat related to some advanced Python concepts which we might not even learn through DataQuest, I think.

It is related to a concept called `Iterators` in Python. Our list `a` is iterable, that is we can iterate over it. Thatâ€™s why `for item in a` is possible.

That `item` is in turn called an `Iterator` which, as I mentioned, is an advanced concept related to Python. `item` gets defined as an iterator, and based on that we can easily have that `for` loop.

But Python does not create an iterator like `item[2]`. Because thatâ€™s essentially mixing two Python operations - creating an iterator, and indexing. Which is really not possible at the same time because `Iterators` donâ€™t really â€śstoreâ€ť all the items from that list at one time. They have to iterate through it. This is just a very high-level overview of what they do.

So, you have to separate out the two steps.

If you are interested in learning a bit more about this, then I would suggest this resource as a decent starting point. If it seems confusing, then donâ€™t worry. Just come back to it later as you get more and more comfortable with Python.

2 Likes

I think then I could say:

I cant do
for c_rating[10] in apps_data

Until I first make a separate variable, in the mission example, row, and then assign it to the values counted first in the row variable?

Also I like the username, favorite Doctor? Mine is the Sixth.

1 Like

Yup, thatâ€™s correct! Just like the first set of code you shared.

You can do the following though -

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

Once you have `row` â€ścreatedâ€ť, you can then use indexing on that for other operations.

favorite Doctor? Mine is the Sixth

Oh, wow. Thatâ€™s a tough one to answer! I have only seen NuWho, though. I am mostly torn between 11 and 12!

1 Like

Hmmmm, ok yeah that makes sense then!

I knew the method in the solution is what I had to do, I just like to try and understand the why

Definitely a tough one to answer! 11 is either my second or third favorite depending on the day, 12 was good but I think he sadly was a little hamstrung by some less than stellar episode writing.

1 Like

Any specific episodes for 12 that you thought were quite bad? In terms of writing or otherwise?