0

I have a set that contains the following :

True
False
42
Thorium
3.1415

When I print the set, I always see that False is listed first followed by True. The next 3 elements are in a random order.

Any reasons for this? I was expecting all the elements to be randomly ordered including booleans when I print the set multiple times.

wovano
  • 4,543
  • 5
  • 22
  • 49
Sachin D
  • 19
  • 4
  • 1
    Does this answer your question? [Why don't Python sets preserve insertion order?](https://stackoverflow.com/questions/61414947/why-dont-python-sets-preserve-insertion-order) – wovano Aug 17 '20 at 19:23
  • How is what you see *not* random? The booleans coming first is one of the possible random outcomes. It's not that the output order is randomized each time you print the set. It's just that there is no guaranteed order you can rely on. – mkrieger1 Aug 17 '20 at 19:25
  • @mkrieger1, if you create a set with the mentioned values and then print that set, the first two printed values are ALWAYS `False` and `True` (well, at least the first 30 times I tried). So it's definitely not just an accidental outcome of a completely random ordering. (But I agree it's not a guaranteed order you can rely on - it's probably implementation dependent.) – wovano Aug 17 '20 at 21:48

0 Answers0