• When are sets of ints ordered?

 

Reading some solution, I noticed that some sets of ints are apparently ordered, depending on size and range. Why? When? And is this documented/guaranteed somewhere? I couldn't find anything.

For example:

>>> import random
>>> for i in range(20, 50):
        L = list(set(random.sample(range(i), 20)))
        print(i, L == sorted(L), L)

20 True [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
21 True [0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
22 True [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
23 True [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 18, 19, 20, 22]
24 True [0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 18, 19, 20, 21, 22, 23]
25 True [2, 4, 5, 6, 7, 8, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
26 True [0, 1, 2, 3, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 18, 19, 20, 21, 23, 25]
27 True [1, 2, 3, 4, 6, 8, 9, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 26]
28 True [0, 4, 5, 7, 8, 9, 10, 11, 13, 14, 16, 17, 18, 19, 20, 23, 24, 25, 26, 27]
29 True [0, 1, 3, 5, 7, 8, 9, 10, 11, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26, 28]
30 True [1, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 22, 24, 25, 26, 28]
31 True [0, 2, 3, 4, 6, 10, 12, 13, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28]
32 True [1, 2, 3, 4, 5, 7, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 27, 31]
33 True [1, 2, 5, 6, 9, 12, 13, 14, 15, 16, 18, 19, 22, 23, 25, 26, 27, 28, 29, 31]
34 False [0, 32, 2, 4, 6, 8, 10, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26, 28, 30, 31]
35 False [1, 34, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 18, 22, 25, 26, 27, 30]
36 False [0, 33, 34, 1, 4, 5, 6, 7, 10, 13, 14, 18, 19, 24, 26, 27, 28, 29, 30, 31]
37 False [0, 1, 34, 35, 5, 6, 7, 8, 9, 11, 12, 13, 14, 16, 18, 19, 20, 21, 23, 31]
38 False [3, 36, 37, 4, 9, 10, 11, 12, 14, 15, 18, 19, 20, 21, 22, 24, 25, 26, 28, 29]
39 False [0, 33, 34, 3, 4, 36, 1, 37, 8, 9, 10, 11, 6, 14, 35, 18, 20, 23, 26, 27]
40 False [1, 34, 35, 2, 4, 37, 33, 9, 11, 12, 14, 16, 17, 18, 20, 25, 28, 29, 30, 31]
41 False [32, 34, 2, 4, 37, 38, 39, 7, 40, 5, 11, 13, 14, 16, 17, 19, 21, 23, 25, 29]
42 False [32, 1, 0, 3, 34, 37, 36, 39, 40, 41, 10, 11, 8, 16, 20, 23, 24, 25, 26, 31]
43 False [32, 1, 35, 4, 37, 39, 40, 7, 10, 11, 42, 14, 15, 16, 17, 18, 23, 25, 29, 31]
44 False [33, 35, 3, 5, 38, 4, 8, 41, 42, 43, 12, 13, 40, 19, 21, 22, 23, 24, 26, 27]
45 False [33, 34, 35, 4, 36, 38, 7, 41, 42, 11, 44, 15, 17, 18, 19, 25, 26, 27, 28, 31]
46 False [0, 34, 35, 36, 37, 3, 7, 8, 6, 5, 43, 40, 45, 14, 18, 22, 23, 24, 27, 31]
47 False [36, 6, 38, 8, 9, 42, 43, 11, 45, 46, 15, 14, 17, 19, 23, 24, 26, 28, 29, 30]
48 False [0, 33, 34, 5, 6, 39, 41, 10, 43, 42, 13, 44, 15, 16, 17, 45, 20, 22, 24, 26]
49 False [2, 34, 36, 5, 6, 39, 7, 41, 10, 11, 45, 14, 47, 17, 19, 20, 21, 24, 29, 30]
.