When running this code the results change as expected since a set is unordered:
my_set_1 = {'a','b','c',}
print([i for i in my_set_1])
That is, multiple runs would give different lists, e.g.
['a', 'c', 'b']
['b', 'a', 'c']
['a', 'c', 'b']
['c', 'b', 'a']
etc.
(Note: You might be getting the same result instead, if you don't have PYTHONHASHSEED=random
, as suggested in the comments. Also, if you are using the console to replicate it, make sure you Rerun the console every time you run the code.)
However, when placing the above code in a for loop the results are rather surprising:
for i in range(10):
my_set_1 = {'a','b','c',}
print([i for i in my_set_1])
# Prints:
# ['a', 'c', 'b']
# ['a', 'c', 'b']
# ['a', 'c', 'b']
# ....
A single run of the for loop will print the same list. Rerunning the for loop can print a different list (e.g. ['c', 'b', 'a']
) but it will still be printed 10 times without changing.
Why doesn't it change?
PYTHONHASHSEED=random
, that's why they can't reproduce. Probably you can note it in your answer. – Camarena