List comprehension

leonardo_13 (1530 points)
6 13 20
asked Dec 21, 2021 in Programming in Python by leonardo_13 (1,530 points)
Why if I write this:

{(x,y) for x in {1, 2, 3} for y in {'a', 'b', 'c'}}

I obtain {(2, 'c'), (3, 'c'), (1, 'c'), (2, 'a'), (3, 'a'), (2, 'b'), (1, 'a'), (3, 'b'), (1, 'b')}

and not {(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c')}?

I'm talking about page 7 of Prof Mancini's slides
126 views

1 Answer

Best answer
andrea.sterbini (172680 points)
511 927 1776
answered Dec 21, 2021 by andrea.sterbini (172,680 points)
selected Dec 21, 2021 by leonardo_13
Sets have no notion of ordering and produce their elements in non-deterministic order.
leonardo_13 (1530 points)
6 13 20
commented Dec 21, 2021 by leonardo_13 (1,530 points)
Ok thank you professor.