Skip to main content

srtlst

a simple, generically type-annotated, sorted list

python_version mypy_checked

usage

Create a sorted list like this:

>>> from srtlst import SortedList
>>> s = SortedList([5, 3, 1])
>>> print(s)
[1, 3, 5]

A SortedList mimics a regular list in most ways, but remains sorted:

>>> s = SortedList([5, 3, 1])
>>> s.extend([6, 2, 4])
>>> print(s)
[1, 2, 3, 4, 5, 6]

You can use add() instead of insert() or append():

>>> s = SortedList([5, 3, 1])
>>> s.add(3)
>>> s.add(2)
>>> print(s)
[1, 2, 3, 3, 5]

If you need your data to be sorted in descending order use the optional reverse parameter:

>>> s = SortedList([1, 2, 3], reverse=True)
>>> s.add(4)
>>> print(s)
[4, 3, 2, 1]

If your data is not inherently sortable, or you want to sort it in a non-default way, you can use SortedListByKey and supply a function to sort it (just like with sorted()):

>>> from srtlst import SortedListByKey
>>> my_function = lambda x: x * (-1) ** x
>>> s = SortedListByKey([1, 2, 3, 4], key=my_function)
>>> print(s)
[3, 1, 2, 4]

SortedListByKey behaves like a SortedList in all other ways, and indeed inherits from it. However, when type checking, a SortedList only accepts values for which a less-than (<) method is defined (__lt__()).

SortedListByKey accepts any type of object, as long as an appropriate key function is provided. The key function must return comparable values for the items in the list.

installation

No surprises here:

$ pip install srtlst

type checking

SortedList and SortedListByKey are type-hinted according to PEP 484 and the srtlst library is itself checked using mypy --strict.

Type hints for both containers can be provided using type arguments, just like with list and other containers:

s: SortedList[int] = SortedList([3, 2, 1])

performance

This library aims to provide a simple sorted list without any dependencies which is ready for use. Some of the list operations are reimplemented to take advantage of the list's sortedness using Python's standard bisect library.

This library should suit your needs if you just want to keep stuff sorted, without having to implement the bookkeeping yourself. However, if your sorting needs arise from the need for performance, you should also consider this library: sortedcontainers.

documentation

Documentation is work in progress. For now the Python's built-in help function can be of service:

>>> from srtlst import SortedList
>>> help(SortedList)

Metadata

Release files for srtlst 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for srtlst 0.2.0
File Size Uploaded
srtlst-0.2.0.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for srtlst 0.2.0
File Interpreter ABI Platform
srtlst-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / srtlst-0.2.0.tar.gz

Download URL srtlst-0.2.0.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
afe71c1b2f667f17d66f303ffc3e0560226615250c8a99e822f565e8bcb3b005
BLAKE2b-256 checksum
How to use checksums
f29bcd7a7627a004cbd2a3b7e32478cf32dda6780bf0f54429802465d9367e39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.2 Darwin/22.4.0

Release files / srtlst-0.2.0-py3-none-any.whl

Download URL srtlst-0.2.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3bfa2a80b3e824708d194ad15452c2ee7e149207dd9246f5f56adbba16d77165
BLAKE2b-256 checksum
How to use checksums
b875c6fb4fba649020c3f11600e82ff0867c2d87f2222b665cba1312257f48bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.2 Darwin/22.4.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page