# Very basic question about Python logic

I am curious how python knows what “row” is in the calculation of “proportion”? I understand “row” is a place-holder and could really be anything, but in this instance, Python does not need me to specify I am looking for the contents of the dictionary and not the keys. How does Python know that? Is it because it is a dictionary and it always pulls the contents? If so, would I need to specify if I wanted to pull or work with the keys?

Just curious.

Thank you!

here is a snipit, the full loop is below:

``````for row in content_ratings:
proportion = content_ratings[row] / total_number_of_apps
``````
``````content_ratings = {'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}
total_number_of_apps = 7197

c_ratings_proportions = {}

for row in content_ratings:
proportion = content_ratings[row] / total_number_of_apps
print('key:', row)
print('Proportion value: ', proportion)
c_ratings_proportions[row] = proportion
print(c_ratings_proportions)
``````

## Consider the following

``````abc = ["a", "b", "c", "d", "e"]
for x in abc:
print(x)
``````
Output
``````a
b
c
d
e
``````

`["a", "b", "c", "d", "e"]` is type of sequence called `list`. `abc` is assigned to the list.

The ordering of sequences starts at index 0 from the left.That is, `"a"` is at index 0.

`for` means for each element in the sequence. That is, the `for` loop will access each element of the sequence - in this example, the sequence is a `list` `["a", "b", "c", "d", "e"]`.

`for` each element `x` in the sequence `["a", "b", "c", "d", "e"]`

• Begins at `index = 0`, such that `x = abc[0] = "a"`
• print each element `x` such that `x = abc[index]` (but we do not need to the indexing)
• `for` loop indirectly increment the `index` by 1.

## Suppose `content_rating = [ [...], [...], [...]]` = list of list

``````for row in content_rating
``````

`for` each element `row` in the sequence `content_rating`.

Here `row` can be label as anything we want (Example, `for x in content_rating` but `x` is not meaningful as `row`.) However, we want to make the code readable. `row` informs the user that `row` contains the row data that is represent by a sequence `list`.

This is because `content_rating` is 2-dimensional `list`.

For illustration purpose (and not Python code):

• `content_rating = [ [...], [...], [...]]` = list of list

• `row` = sequence `list` = `[...]`

## In your example, `content_rating` is a dictionary

`content_rating` is a dictionary `{'4+': 4433, '12+': 1155, '9+': 987, '17+': 622}`. And, `for row in content_rating` is not the appropriate way to iterate a dictionary.

`for row in content_rating` means `for` each element `row` of dictionary `content_rating` key values `['4+', '12+', '9+', '17+']`. `content_rating[row]` access the value for particular key in the dictionary.

Label `row` for each element makes the code not as readable since there is no row but we actually meant key.

## Edit code in your example

``````for k, v in content_rating.items():
``````

`.items()` converts into a sequence `list` of `tuple` `(key, value)` pair . That is, `.items()` = `[(key1, value1), ...., (keyn, valuen)]`

`k, v` gets assign to each element `(key, value)`

where `k` means key of the dictionary
and, where `v` means value of the dictionary

And replace

``````content_ratings[row]
``````

with

``````v
``````

You can simplified the code by using a dictionary comprehension

``````c_ratings_proportions = { k: v/total_number_of_apps for k, v in content_ratings.items()}
``````

`.items()` converts into a sequence `list` of `tuple` `(key, value)` pair . That is, `.items()` = `[(key1, value1), ...., (keyn, valuen)]`

`k, v` gets assign to each element `(key, value)`

where `k` means key of the dictionary
and, where `v` means value of the dictionary

dictionary[key] = value / total_number_of_apps, for each element (key, value) in dictionary content_ratings

## Other questions on data structures

Dictionary `{` `}` are not sequences.

• Uses key to access the value
• When using a `for` loop to iterate dictionary values, use `.items()` to convert into a sequence `list` of `tuple` `(key, value)` for `for` loop iterating each element.

Sequence, for example `list` `[` `]`, or `tuple` `( , )` (tuples can contains 1 or more elements separated by a comma `,`)

• Uses index to access elements in the sequences
• Sequences start index at 0.

There are different types of data structures. Read up on data structures in Python Tutorial.

You need to read up on Dictionary. Here’s the dictionary section in Python Tutorial.