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 