Python Interpretation of Indices

Quick question:

How does python ‘know’ which number corresponds to the proper index when it executes the code below? That is, how does it distinguish between the dummy variables I have used?

Would this block of code still function if I used i2 < i1 ?

Furthermore, how does python understand how to parse the code when I include 3 indices?

Hi Taylor,

Regarding the first question, think of the FOR Loops for a second, When you code for X in LIST, the X assumes each individual value of that list and make some kind of operation.
In the example you mentioned, it’s the same thing, but X is not a singular value, but another list. In that case, you must match the size of X with the size of the element you want to assume, and the order matters!!!
Making another example, if you had:
list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
you should make: for (a, b, c) in list.
In that case, in the first iteration, a=1, b=2, c=3.

The second question: Would this block of code still function if I used i2 < i1 ?
Yes. It would be the same thing. The operation doens’t change the assumed values. The for (i1, i2) does.

The third question how does python understand how to parse the code when I include 3 indices?. I think you thought one thing but that is not what the code is doing. You see the in zip(a,b), it is a built-in function that takes 2 lists and create another one with touples from the values with the same index from each one. In this case, this list would be:
[(30, 15), (42, 16), (10, 17)]. So it is the same example as the first.

Hope I helped. If you need anything else, just ask.

Hi again, @m.cassani. Thanks for another detailed explanation! I think I understand, but one follow up question: you mention that ‘you must match the size of X to the size of the element you want to assume’, by this do you mean that if I did not have the zip(a, b) function that I would have had to had (referring to the second image) a1 > b1 for (a1, b1, c1) in (a, b) ? The way I have done this seems wrong, though.
Also, you said that order matters - why is it then that i2 < i1 would be executed in the same way as i1 > i2 ?

Thanks again for the help!

Hi Taylor,

I am thinking you understand the zip function, right? Taking the example:
a = [30, 42, 10]
b = [15, 16, 17]
zip(a, b) = [(30, 15), (42, 16), (10, 17)]
You are using the list created by the zip function, not list a or list b.

The sub-lists (touples in this case) have 2 variables, that’s why you use for (a1, b1), because you need temporary variables to assume both values of the sub-list in each index.
In the first iteration you would have a1 = 30 and b1 = 15.
In the second iteration you would have a1 = 42 and b1 = 16.
In the third iteration you would have a1 = 10 and b1 = 17.

So, if you don’t use zip in the second example, you don’t have a list of pairs. You would only have list a and list b separated, so it wouldn’t be possible to perform any operation comparing a value from one list and a value from another list with List Comprehension. You could only take values from list a or from list b.

However, I get where you are having doubts, so I will make 3 examples regarding the size of X.

list_1 = [10, 20, 30]
if you take list[0] you have 10. So its a single value. So X is a single value as well.
You would have for a in list_1

list_2 = [[10, 20] , [30, 40] , [50, 60]]
if you take list[0] you have [10, 20]. So its a pair of values. So X is a pair of values as well.
You would have for (a, b) in list_2

list_3 = [[10, 20, 30] , [40, 50, 60] , [70, 80, 90]]
if you take list[0] you have [10, 20, 30]. So its a set of three values. So X is a set of three values as well.
You would have for (a, b, c) in list_3

Just remember, to get the size of X, take one index of the list as an example: like print(list[0])

Regarding the second question: Also, you said that order matters - why is it then that i2 < i1 would be executed in the same way as i1 > i2 ?
The order matters when you are assuming values with your variables, not in the operation you are performing. See:
a = [30, 42, 10]
b = [15, 16, 17]
zip(a, b) = [(30, 15), (42, 16), (10, 17)]
greater_than = [a1 > b1 for (a1, b1) in zip(a, b)] → The order matters here: for (a1, b1)

a1 will assume the first value of the pair and b1 the second value.
In the first iteration you would have a1 = 30 and b1 = 15.
So a1 > b1 will be True and b1 < a1 will be also True.

However, if you do greater_than = [a1 > b1 for (b1, a1) in zip(a, b)].
In the first iteration you would have b1 = 30 and a1 = 15.
So a1 > b1 will be False and b1 < a1 will be also False.

Hope this helped :slight_smile:

Ahhh. This makes so much sense now! I see what you meant previously when you stated “you must match the size of X with the size of the element you want to assume”. So the zip() function is ‘outputting’ the touples, so we need not be concerned with the fact that the original lists have three elements (or any number of elements, for that matter).

Thank you for explaining this so clearly.

1 Like

I am really glad that I was able to help.

Keep coding and keep asking questions. It’s the best way to learn.

1 Like