Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is python's "set" stable?

Tags:

python

set

The question arose when answering to another SO question (there).

When I iterate several times over a python set (without changing it between calls), can I assume it will always return elements in the same order? And if not, what is the rationale of changing the order ? Is it deterministic, or random? Or implementation defined?

And when I call the same python program repeatedly (not random, not input dependent), will I get the same ordering for sets?

The underlying question is if python set iteration order only depends on the algorithm used to implement sets, or also on the execution context?

like image 363
kriss Avatar asked Sep 28 '10 12:09

kriss


People also ask

How efficient are Python sets?

Conclusion. The Python sets are highly useful to efficiently remove duplicate values from a collection like a list and to perform common math operations like unions and intersections.

Are Python sets unchangeable?

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage. A set is a collection which is unordered, unchangeable*, and unindexed. * Note: Set items are unchangeable, but you can remove items and add new items.

Are Python sets slow?

Sets are significantly faster when it comes to determining if an object is present in the set (as in x in s ), but its elements are not ordered so you cannot access items by index as you would in a list. Sets are also somewhat slower to iterate over in practice.

What is unique about a Python set?

A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).


1 Answers

There's no formal guarantee about the stability of sets. However, in the CPython implementation, as long as nothing changes the set, the items will be produced in the same order. Sets are implemented as open-addressing hashtables (with a prime probe), so inserting or removing items can completely change the order (in particular, when that triggers a resize, which reorganizes how the items are laid out in memory.) You can also have two identical sets that nonetheless produce the items in different order, for example:

>>> s1 = {-1, -2} >>> s2 = {-2, -1} >>> s1 == s2 True >>> list(s1), list(s2) ([-1, -2], [-2, -1]) 

Unless you're very certain you have the same set and nothing touched it inbetween the two iterations, it's best not to rely on it staying the same. Making seemingly irrelevant changes to, say, functions you call inbetween could produce very hard to find bugs.

like image 196
Thomas Wouters Avatar answered Sep 29 '22 12:09

Thomas Wouters