3

I have searched all over for an answer to this, however, every site tells me what I already know - that sets are an unordered collection of values. I don't dispute this in any way.

What makes no sense to me and what I can't get my head around, is when creating a set of the integers 1, 3, 5, and 7 in Python3.9 they are always retrieved/printed in order. Other sets of ints are stored unordered as expected (as far as I've checked). I suppose what I'm asking is, is this a quirk of Python, a misnomer of set theory, or just an incredibly marginal coincidence?

I came across this quirk in code different to that of below, however, I had to check all possible ways of creating the set and that's what I've shared:

listOfSets =    [
                {1, 3, 5, 7},
                {1, 3, 7, 5},
                {1, 5, 3, 7},
                {1, 5, 7, 3},
                {1, 7, 5, 3},
                {1, 7, 3, 5},
                {7, 3, 5, 1},
                {7, 3, 1, 5},
                {7, 5, 3, 1},
                {7, 5, 1, 3},
                {7, 1, 5, 3},
                {7, 1, 3, 5},
                {5, 3, 1, 7},
                {5, 3, 7, 1},
                {5, 1, 3, 7},
                {5, 1, 7, 3},
                {5, 7, 1, 3},
                {5, 7, 3, 1},
                {3, 1, 5, 7},
                {3, 1, 7, 5},
                {3, 5, 1, 7},
                {3, 5, 7, 1},
                {3, 7, 5, 1},
                {3, 7, 1, 5},
                ]

for x in listOfSets:
    print(x)

Output:

{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
{1, 3, 5, 7}
  • 1
    this is the result of an implementation detail, that most int's hash to themselves. – juanpa.arrivillaga Nov 07 '20 at 03:28
  • 1
    There are no guarantees about this, and for large integers they won't always be sorted, but smaller integers are their own hash values. E.g. `hash(1)` is `1`, `hash(2)` is `2`, etc. So when the hash table is created, it is natural for them to be in the same order (up to a point). Note that for dictionaries, the order in which the elements are added determines the externally visible dict order, so this doesn't apply to them. – Tom Karzes Nov 07 '20 at 03:30
  • @TomKarzes and because the hash is taken as a modulo of the internal table size. – juanpa.arrivillaga Nov 07 '20 at 03:31
  • @juanpa.arrivillaga right, so you can't always rely on that order. And in fact, it's possible that some day they may change sets to behave like dicts, in which case it would depend on the order in which the elements were added. – Tom Karzes Nov 07 '20 at 03:32
  • Thank you @TomKarzes, that's really interesting. – Michael Ritchie Nov 07 '20 at 03:39
  • I'm going to read up on implementation details as well, thanks @juanpa.arrivillaga – Michael Ritchie Nov 07 '20 at 03:40

0 Answers0