2

I was experimenting with set in python and while I understood that it is unsorted, based on hashes, I find it strange that it automatically sorts these numbers, both in Python 2 and in 3:

>>> list(set([2,1,3,6,5,3,6,4]))
[1, 2, 3, 4, 5, 6]
>>> list(set([2,1,3,6,5,3,6,4,0,7]))
[0, 1, 2, 3, 4, 5, 6, 7]

I googled for some time but didn't find the answer to the behavior of these two functions combined.

Adelin
  • 7,809
  • 5
  • 37
  • 65

2 Answers2

3

It does not, here is an example

In [2]: list(set([1,9,5,10]))                                                                                                                                                                                                
Out[2]: [1, 10, 5, 9]

Also, from the docs: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset

A set object is an unordered collection of distinct hashable objects.

The reason sometimes you see the sorted output is dependent on how the hash is computed, and sometimes on what REPL is being used, and this behaviour is well described in this answer

Example for ipython, the way the set is printed, changes when we enable doctest_mode, which disables pretty-printing of ipython

In [1]: set([1,6,8,4])                                                                                                                                                                                                       
Out[1]: {1, 4, 6, 8}

In [2]: %doctest_mode                                                                                                                                                                                                        
Exception reporting mode: Plain
Doctest mode is: ON
>>> set([1,6,8,4])                                                                                                                                                                                                           
{8, 1, 4, 6}
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • Strange, yet if I add more numbers, it does: `list(set([2,1,3,6,5,3,6,4,0,7,10,9]))` -> `[0, 1, 2, 3, 4, 5, 6, 7, 9, 10]` – Adelin May 17 '19 at 11:34
  • If the implementation uses a binary tree or similar data structure, rather than a hash table, then the results will be ordered. But you should not assume that to be the case since the documentation explicitly says it is not ordered. – Kurtis Rader May 18 '19 at 02:08
0

This is not a feature of sets and only a coincidence stemming from how sets are created. The hash of the items in your list are the numbers themselves. So under some circumstances the created set will show this behaviour but is in no way reliable.
Looking at this answer you can read more about it.

NoSplitSherlock
  • 605
  • 4
  • 19