Skip to content

Python sorted dictionary: a Python dictionary in which the keys are always in ascending order

License

Notifications You must be signed in to change notification settings

tfpf/pysorteddict

pysorteddict

Provides SortedDict, which is a Python sorted dictionary: a Python dictionary in which the keys are always in ascending order.

Hatch project ci

Documentation

View the documentation on GitHub Pages.

Installation

pysorteddict is available on PyPI. It requires Python 3.10 or newer. Built distributions (binary wheels) are provided for 64-bit 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.

Usage

All keys in a sorted dictionary must be of the same type, which is determined when the first key-value pair is inserted into it. The values, though, can be of any type.

import json

from pysorteddict import SortedDict

sorted_dict = SortedDict()
sorted_dict["honestly"] = "weight"
sorted_dict["gain is"] = 31.692
sorted_dict["times"] = "easier than"
sorted_dict["losing"] = ["weight"]
assert sorted_dict.key_type is str
print(json.dumps(sorted_dict, indent=2, sort_keys=False))

The above Python script will output the keys in ascending order.

{
  "gain is": 31.692,
  "honestly": "weight",
  "losing": [
    "weight"
  ],
  "times": "easier than"
}

The following key types are supported.

  • bytes
  • float
  • int
  • str

Implementation Details

pysorteddict is implemented entirely in C++. SortedDict provides a Python interface to std::map<PyObject*, PyObject*, _>.

About

Python sorted dictionary: a Python dictionary in which the keys are always in ascending order

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks