Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is set.pop() deterministic?

Tags:

I understand that the elements of a python set are not ordered. Calling the pop method returns an arbitrary element; I'm fine with that.

What I'm wondering is whether or not pop will ALWAYS return the same element when the set has the same history. Within one version of python of course, I don't mind if different versions/implementations of python do their own thing. In particular, I'm asking about python 2.7. It's a matter of implementation more than of api in this case.

I'm using sets a lot in a procedural dungeon generator for a game, and I'd like the outcome to be deterministic for a given seed.

like image 833
Niriel Avatar asked May 03 '12 13:05

Niriel


People also ask

Does pop work on set?

Definition and Usage. The pop() method removes a random item from the set. This method returns the removed item.

Is set pop random?

The set pop() method returns the value which is popped. The value is popped randomly. If the set is empty, it returns a TypeError exception.

What does Pop do in set?

Python Set | pop() Python set pop() Method removes any a random element from the set and returns the removed element.

Does set have pop Python?

The Python built-in type set has a method called pop(), from the docs: Remove and return an arbitrary element from the set.


2 Answers

The answer in general is no. The python source that @Christophe and @Marcin (un)helpfully point to shows that elements are popped in the order they appear in the hash table. So, pop order (and presumably iteration order) is deterministic, but only for fixed hash values. That's the case for numbers but not for strings, according to the Note in the documentation of __hash__, which incidentally also touches on your question directly:

Note by default the hash() values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.

[ ... ]

Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).

Edit: As @Marcin points out, the link I quoted does not apply to Python 2. Hash randomization became the default with Python 3.3. Python 2.7 does not have intentionally non-deterministic string hashing by default.

In general, this is a problem for any object whose hash is not a repeatable function of its value (e.g., if the hash is based on memory address). But conversely, if you define your own __hash__ method for the objects in your sets, you can expect that they will be returned in a reproducible order. (Provided the set's history and the platform are kept fixed).

like image 199
alexis Avatar answered Sep 20 '22 08:09

alexis


Internally I think the situation is similar to dict. The order is determined by an hash algorithm, which in some situations will yield the same results. But you should not depend on that, since once the number of elements gets large, the set will encounter collisions (that is it's internal hashing), which eventually lead to a different ordering.

In short: No, set.pop() is not deterministic. Don't assume any order, since the API explicitly states, that

a set object is an unordered collection

  • Docs: http://docs.python.org/library/stdtypes.html#set-types-set-frozenset
  • Source: http://svn.python.org/view/python/trunk/Objects/setobject.c?view=markup
like image 40
miku Avatar answered Sep 21 '22 08:09

miku