I understand most of this:
Please use small words. I'm a beginner!
Wait, I think I figured it out. It's already a tuple, and it's in a list. So a new tuple is made every time the conditions of the loops are met?
1 |
>>> [(x, y) for x in [ 1 , 2 , 3 ] for y in [ 3 , 1 , 4 ] if x ! = y] |
Output:[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Equivalent to:1 2 3 4 5 6 7 8 9 |
>>> >>> combs = [] >>> for x in [ 1 , 2 , 3 ]: ... for y in [ 3 , 1 , 4 ]: ... if x ! = y: ... combs.append((x, y)) ... >>> combs |
Output:[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
What I don't understand is how, in the list comprehension, Python knows to make (x, y) into a list of tuples. Please use small words. I'm a beginner!
Wait, I think I figured it out. It's already a tuple, and it's in a list. So a new tuple is made every time the conditions of the loops are met?