title: Why is Python's set() sorting a list in some cases?
body: <p>I'm confused by the behavior of Python's <code>set()</code> in this example:</p>
  <pre class="lang-py prettyprint-override"><code>random_number_list = [randint(1, 10) for _ in range(10)]
  # This will be sorted!
  unique_numbers = set(random_number_list)
  
  print(
      f&quot;random_number_list/unique_numbers with same upper bound for randint() and range():\n{random_number_list=}\n{unique_numbers=}\n&quot;
  )
  
  random_number_list = [randint(1, 100) for _ in range(10)]
  # This will not be sorted.
  unique_numbers = set(random_number_list)
  
  print(
      f&quot;random_number_list/unique_numbers with different upper bound for randint() and range():\n{random_number_list=}\n{unique_numbers=}\n&quot;
  )
  </code></pre>
  <p>It seems like <code>set()</code> is sorting the <code>random_number_list</code> if the length of the list and the upper bound of <code>randint()</code> are the same:</p>
  <pre><code>➜  ch-2 python --version
  Python 3.10.0
  ➜  ch-2 python find_k_smallest.py 
  random_number_list/unique_numbers with same upper bound for randint() and range():
  random_number_list=[10, 1, 2, 5, 5, 7, 8, 8, 2, 8]
  unique_numbers={1, 2, 5, 7, 8, 10}
  
  random_number_list/unique_numbers with different upper bound for randint() and range():
  random_number_list=[35, 1, 17, 26, 17, 74, 26, 11, 44, 13]
  unique_numbers={1, 35, 74, 11, 44, 13, 17, 26}
  </code></pre>
  <p>The <a href="https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset" rel="nofollow noreferrer">docs</a> state:</p>
  <blockquote>
  <p>A set object is an unordered collection of distinct hashable objects.</p>
  </blockquote>
  <p>What's going on here? Why is <code>set()</code> sorting the <code>random_number_list</code> in certain cases and not others?</p>
  <p><em><strong>Edit</strong></em> Neither of these questions addresses my issue:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/63845073/my-python-multiprocesses-are-apparently-not-independent/64866191#64866191">my Python multiprocesses are apparently not independent</a></li>
  <li><a href="https://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set">Does Python have an ordered set?</a></li>
  </ul>