Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set iteration order varies from run to run

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?

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.

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.

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.

The best solution I've come up with is:

  1. Copy the set to a list.
  2. Apply an arbitrary sort to the list.
  3. Iterate the list instead of the set.

Is there a simpler solution?

Note: I've found similar questions on StackOverlow, but none that address this specific issue of getting the same results from run to run.

like image 564
Adrian McCarthy Avatar asked Oct 03 '10 00:10

Adrian McCarthy


People also ask

Does set iterate in order?

The set's iteration order depends not only its contents, but on the order in which the items were inserted into the set, and whether there were deletions along the way.

Does Python set guarantee order?

The answer is no, but you can use collections. OrderedDict from the Python standard library with just keys (and values as None ) for the same purpose. Update: As of Python 3.7 (and CPython 3.6), standard dict is guaranteed to preserve order and is more performant than OrderedDict .

Is it faster to iterate through list or set python?

Iterating over a List is much much faster than iterating over a set. The currently accepted answer is using a very small set and list and hence, the difference is negligible there.

Are set iterable?

In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements.


1 Answers

The reason the set iteration order changes from run-to-run appears to be because Python uses hash seed randomization by default. (See command option -R.) Thus set iteration is not only arbitrary (because of hashing), but also non-deterministic (because of the random seed).

You can override the random seed with a fixed value by setting the environment variable PYTHONHASHSEED for the interpreter. Using the same seed from run to run means set iteration is still arbitrary, but now it is deterministic, which was the desired property.

Hash seed randomization is a security measure to make it difficult for an adversary to feed inputs that will cause pathological behavior (e.g., by creating numerous hash collisions). For unit testing, this is not a concern, so it's reasonable to override the hash seed while running tests.

like image 152
Adrian McCarthy Avatar answered Sep 23 '22 11:09

Adrian McCarthy