Provides SortedDict
, which is a Python sorted dictionary: a Python dictionary in which the keys are always in
ascending order.
pysorteddict is available on PyPI. It requires Python 3.10 or newer. Built distributions (binary wheels) are provided for Linux, macOS and Windows, so installing is straightforward.
pip install pysorteddict
If you are on any other platform, install the Python development headers and libraries before running the above command.
All keys in a sorted dictionary must be of the same type, which must be passed to the constructor. The values, though, can be of any type.
from pysorteddict import SortedDict
sorted_dict = SortedDict(int)
sorted_dict[5659] = "gaining weight is"
sorted_dict[1992] = 31.692
sorted_dict[24274] = "times easier than"
sorted_dict[9765] = ["losing", "weight"]
print(sorted_dict)
This program should output
{1992: 31.692, 5659: 'gaining weight is', 9765: ['losing', 'weight'], 24274: 'times easier than'}
. Note that the keys
are in ascending order.
pysorteddict is implemented entirely in C++. SortedDict
provides a Python interface to
std::map<PyObject*, PyObject*, _>
.
Create a new sorted dictionary in which the keys are of type key_type
. As of the current version, key_type
must be
int
. Support for some more types will be added in due course.
Return whether key
is present in the sorted dictionary d
.
Return the number of key-value pairs in the sorted dictionary d
.
Return the value mapped to key
in the sorted dictionary d
.
- If
type(key)
is not the same askey_type
passed to the constructor, raiseTypeError
. - If
key
is not present ind
, raiseKeyError
.
Map value
to key
in the sorted dictionary d
, replacing the previously-mapped value (if any).
- If
type(key)
is not the same askey_type
passed to the constructor, raiseTypeError
.
Remove key
and the value mapped to it from the sorted dictionary d
.
- If
type(key)
is not the same askey_type
passed to the constructor, raiseTypeError
. - If
key
is not present ind
, raiseKeyError
.
Return a human-readable representation of the sorted dictionary d
.
Remove all key-value pairs in the sorted dictionary d
.
Return a shallow copy of the sorted dictionary d
.
Create and return a new list containing the key-value pairs in the sorted dictionary d
. This list will be sorted.
Create and return a new list containing the keys in the sorted dictionary d
. This list will be sorted.
Create and return a new list containing the values in the sorted dictionary d
. This list will be sorted by the keys
which the values are mapped to.