Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are sets ordered like dicts in python3.6

Due to changes in dict implementation in Python 3.6 it is now ordered by default. Do sets preserve order as well now?

I could not find any information about it but as both of those data structures are very similar in the way they work under the hood I thought it might be the case.

I know there is no promise for dicts to be ordered in all cases but they are most of the time. As stated in Python docs:

The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon

like image 353
Quba Avatar asked Aug 09 '17 04:08

Quba


People also ask

Are Python3 sets ordered?

Sets are unordered. Set elements are unique. Duplicate elements are not allowed. A set itself may be modified, but the elements contained in the set must be of an immutable type.

Are sets in Python sorted?

Short answer from python doc. A set is an unordered collection with no duplicate elements. Since sets only define partial ordering (subset relationships), the output of the list. sort() method is undefined for lists of sets.

Are all Dicts ordered Python?

Ordered or Unordered? As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered. When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.

Is a Python set always ordered?

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 .


1 Answers

No, sets are still unordered.

You can verify this just by displaying a set that should have a "well-defined hash order"1 to make sure we don't accidentally get a set that looks ordered but actually isn't:

>>> a_set = {3,2,1} >>> a_set {1, 2, 3} >>> list(a_set) [1, 2, 3] 

If it were ordered you would expect {3, 2, 1} and [3, 2, 1] as result of the examples.

While dicts are actually ordered (same example just a bit modified):

>>> a_dict = {3: 3, 2: 2, 1:1} >>> a_dict {3: 3, 2: 2, 1: 1} >>> list(a_dict) [3, 2, 1] 

1 "well-defined hash order":

For integers that satisfy 0 <= integer < sys.hash_info.modulus the hash is just the number itself. That means if the set is ordered "based" on the hash (and not ordered based on the insertion "time") and the hash values don't collide (that's why I used small numbers and numbers that only differ by one) the order should be deterministic because they occupy slots inside the set that are next to each other:

  • Either from smallest to highest
  • or a from a specific value to the highest and then from the smallest to the specific value. This case happens if the next (in the sense of neighboring) free slot in the set is the first one.

As an example for the latter:

>>> a_set = {6,7,8,9} >>> a_set {8, 9, 6, 7} 
like image 90
MSeifert Avatar answered Sep 23 '22 01:09

MSeifert