Is there a Python design decision (PEP) that precludes a sorted container from being added to Python?
(OrderedDict
is not a sorted container since it is ordered by insertion order.)
Is there a Python design decision (PEP) that precludes a sorted container from being added to Python?
(OrderedDict
is not a sorted container since it is ordered by insertion order.)
It's a conscious design decision on Guido's part (he was even somewhat reluctant regarding the addition of the collections
module). His goal is to preserve "one obvious way to do it" when it comes to the selection of data types for applications.
The basic concept is that if a user is sophisticated enough to realise that the builtin types aren't the right solution for their problem, then they're also up to the task of finding an appropriate third party library.
Given that list+sorting, list+heapq and list+bisect cover many of the use cases that would otherwise rely on inherently sorted data structures, and packages like blist exist, there isn't a huge drive to add more complexity in this space to the standard library.
In some ways, it is similar to the fact that there's no multi-dimensional array in the standard library, instead ceding that task to the NumPy folks.
collections.Counter
can be used as sorted set. Though it may not be efficient. –
Camber collections.Counter
is unsorted and not appropriate for representing a sorted set. –
Fantoccini dict
is a hash table. –
Knorr There's also a python sortedcontainers module that implements sorted list, dict, and set types. It's very similar to blist but implemented in pure-Python and in most cases faster.
>>> from sortedcontainers import SortedSet
>>> ss = SortedSet([3, 7, 2, 2])
>>> ss
SortedSet([2, 3, 7])
It also has functionality uncommon to other packages:
>>> from sortedcontainers import SortedDict
>>> sd = SortedDict((num, num) for num in range(100000))
>>> sd.iloc[-5] # Lookup the fifth-to-last key.
99995
Disclosure: I am the author of the sortedcontainers module.
update()
? Have just encountered that unintuitive side effect! - Answer seems use add() instead! –
Marche SortedSet(set('abracadabra'))
. I'm not sure the right answer about typing things. –
Aludel It's a conscious design decision on Guido's part (he was even somewhat reluctant regarding the addition of the collections
module). His goal is to preserve "one obvious way to do it" when it comes to the selection of data types for applications.
The basic concept is that if a user is sophisticated enough to realise that the builtin types aren't the right solution for their problem, then they're also up to the task of finding an appropriate third party library.
Given that list+sorting, list+heapq and list+bisect cover many of the use cases that would otherwise rely on inherently sorted data structures, and packages like blist exist, there isn't a huge drive to add more complexity in this space to the standard library.
In some ways, it is similar to the fact that there's no multi-dimensional array in the standard library, instead ceding that task to the NumPy folks.
collections.Counter
can be used as sorted set. Though it may not be efficient. –
Camber collections.Counter
is unsorted and not appropriate for representing a sorted set. –
Fantoccini dict
is a hash table. –
Knorr Not exactly a "sorted container", but you might be interested in the standard library's bisect module, which "provides support for maintaining a list in sorted order without having to sort the list after each insertion".
There is a heapq
in the standard library, it is not exactly sorted, but kind of. There is also a blist package, but it is not in the standard library.
For the specific case of a sorted set I find Flag
useful, e.g.:
from enum import Flag
Color = Flag('Color', 'RED GREEN BLUE')
This can be used like a set, |
is union, &
is intersection, and ~
is inversion, e.g.:
set1 = Color.RED | Color.GREEN
set2 = Color.BLUE
union = set1 | set2
intersection = set1 & set2
inversion = ~set1
empty = Color(0)
universal = ~empty
print(universal)
Which prints:
Color.RED|GREEN|BLUE
The sets are automatically sorted in declaration order (point of discussion w.r.t. sets) and the universal set is closed (which I like).
Python lists are ordered. If you sort them, they stay that way. In Python 2.7 an OrderedDict
type was added to maintain an explicitly ordereded dictionary.
Python also has sets (a collection in which the members must be unique), but by definition they are unordered. Sorting a set just returns a list
.
© 2022 - 2025 — McMap. All rights reserved.