title: Set iteration order varies from run to run
body: <p>Why does the iteration order of a Python set (with the same contents) vary from run to run, and what are my options for making it consistent from run to run?</p>
  
  <p>I understand that the iteration order for a Python set is arbitrary.  If I put 'a', 'b', and 'c' into a set and then iterate them, they may come back out in any order.</p>
  
  <p>What I've observed is that the order remains the same within a run of the program.  That is, if my program iterates the same set twice in a row, I get the same order both times.  However, if I run the program twice in a row, the order changes from run to run.</p>
  
  <p>Unfortunately, this breaks one of my automated tests, which simply compares the output from two runs of my program.  I don't care about the actual order, but I would like it to be consistent from run to run.</p>
  
  <p>The best solution I've come up with is:</p>
  
  <ol>
  <li>Copy the set to a list.</li>
  <li>Apply an arbitrary sort to the list.</li>
  <li>Iterate the list instead of the set.</li>
  </ol>
  
  <p>Is there a simpler solution?</p>
  
  <p>Note: I've found similar questions on StackOverlow, but none that address this specific issue of getting the same results from run to run.</p>