0

I noticed that constructing a set from a given list of numbers in order to contain only unique numbers returns always a sorted set. I read that a set is not ordered, so what am I missing here?

I run this simple code a few times, printing always an orderd list:

import random
random_list = [random.randrange(1, 100) for i in range(1000)]
print(set(random_list))
Natjo
  • 2,005
  • 29
  • 75
  • Does this answer your question? [Does Python have an ordered set?](https://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set) – quamrana Mar 04 '20 at 16:18
  • 1
    How did you check that the set output is sorted ? It should not be. Did you just manually checked it ? – PraveenB Mar 04 '20 at 16:27
  • 1
    You're not really "missing" anything. Any observed ordering is an implementation detail, behaviour which is not guaranteed to be consistent across python versions or implementations, or even in a particular version. – snakecharmerb Mar 04 '20 at 16:31
  • 1
    Sets are *arbitrarily* ordered, not *un*-ordered. Your sampled data just happens to be ordered in a way you perceive as natural. Try ``{9223372036854775804, 1}`` instead. – MisterMiyagi Mar 04 '20 at 16:46
  • @PraveenB, yes, I just manually checked the output a few times. Since I assumed a completely random order, an ordered list of numbers seemed too specific to happen by random. – Natjo Mar 05 '20 at 08:27

1 Answers1

1

The set's internal order (of hash buckets) is mostly determined by the elements' hash value. For int objects n, it holds that

n == hash(n)

so the set's inner structure will closely mirror the natural order of the elements. If you try the same with floats, you will see a different behaviour:

random_list = [random.randrange(1, 100) / 4.0 for i in range(10)]
print(set(random_list))
# {3.5, 4.75, 4.25, 6.25, 5.5, 11.25, 13.25, 16.25, 19.0, 23.75}
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • I think it also has to do with the number of random number. When I run OP's code with `for i in range(10)`, the set is not ordered anymore – MT756 Mar 04 '20 at 16:23
  • 1
    @MT756 Yes when the set set is smaller then the range of numbers picked from, the large ones are mapped to smaller hash buckets, randomizing the order. But if the set contains contiguous numbers from 1 to n, each will get its own hash value's bucket. – user2390182 Mar 04 '20 at 16:55