Skip to main content

Python SortedContainers

https://api.travis-ci.org/grantjenks/sorted_containers.svg

SortedContainers is an Apache2 licensed sorted collections library, written in pure-Python, and fast as C-extensions.

Python’s standard library is great until you need a sorted collections type. Many will attest that you can get really far without one, but the moment you really need a sorted list, dict, or set, you’re faced with a dozen different implementations, most using C-extensions without great documentation and benchmarking.

In Python, we can do better. And we can do it in pure-Python!

>>> sl = sortedcontainers.SortedList(xrange(10000000))
>>> 1234567 in sl
True
>>> sl[7654321]
7654321
>>> sl.add(1234567)
>>> sl.count(1234567)
2
>>> sl *= 3
>>> len(sl)
30000003

Note: don’t try this without at least a half gigabyte of memory. In Python an integer requires about 24 bytes. SortedList will add about 8 bytes per object stored in the container. That’s pretty hard to beat as it’s the cost of a pointer to each object. It’s also 66% less overhead than a typical binary tree implementation (e.g. red-black tree, avl tree, aa tree, splay tree, treap, etc.) for which every node must also store two pointers to children nodes.

SortedContainers takes all of the work out of Python sorted collections - making your deployment and use of Python easy. There’s no need to install a C compiler or pre-build and distribute custom extensions. Performance is a feature and testing has 100% coverage with unit tests and hours of stress.

Testimonials

Alex Martelli, Wikipedia

Good stuff! … I like the simple, effective implementation idea of splitting the sorted containers into smaller “fragments” to avoid the O(N) insertion costs.

Jeff Knupp, Review of SortedContainers

That last part, “fast as C-extensions,” was difficult to believe. I would need some sort of Performance Comparison to be convinced this is true. The author includes this in the docs. It is.

Kevin Samuel, Formations Python

I’m quite amazed, not just by the code quality (it’s incredibly readable and has more comment than code, wow), but the actual amount of work you put at stuff that is not code: documentation, benchmarking, implementation explanations. Even the git log is clean and the unit tests run out of the box on Python 2 and 3.

Mark Summerfield, a short plea for Python Sorted Collections

Python’s “batteries included” standard library seems to have a battery missing. And the argument that “we never had it before” has worn thin. It is time that Python offered a full range of collection classes out of the box, including sorted ones.

Features

  • Pure-Python

  • Fully documented

  • Benchmark comparison (alternatives, runtimes, load-factors)

  • 100% test coverage

  • Hours of stress testing

  • Performance matters (often faster than C implementations)

  • Compatible API (nearly identical to popular blist and rbtree modules)

  • Feature-rich (e.g. get the five largest keys in a sorted dict: d.iloc[-5:])

  • Pragmatic design (e.g. SortedSet is a Python set with a SortedList index)

  • Developed on Python 2.7

  • Tested on CPython 2.7, 3.2, 3.3, 3.4, 3.5, 3.6 and PyPy, PyPy3

Quickstart

Installing SortedContainers is simple with pip:

$ pip install sortedcontainers

You can access documentation in the interpreter with Python’s built-in help function:

>>> from sortedcontainers import SortedList, SortedSet, SortedDict
>>> help(SortedList)

Documentation

Complete documentation including performance comparisons is available at http://www.grantjenks.com/docs/sortedcontainers/ .

User Guide

For those wanting more details, this part of the documentation describes introduction, implementation, performance, and development.

API Documentation

If you are looking for information on a specific function, class or method, this part of the documentation is for you.

Talks

Contribute

Collaborators are welcome!

  1. Check for open issues or open a fresh issue to start a discussion around a bug. There is a Contributor Friendly tag for issues that should be used by people who are not very familiar with the codebase yet.

  2. Fork the SortedContainers repository on GitHub and start making your changes to a new branch.

  3. Write a test which shows that the bug was fixed.

  4. Send a pull request and bug the maintainer until it gets merged and published.

SortedContainers License

Copyright 2014-2016 Grant Jenks

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Metadata

Release files for sortedcontainers 1.5.9

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

Source distribution (sdist)

Source distribution for sortedcontainers 1.5.9
File Size Uploaded
sortedcontainers-1.5.9.tar.gz 12.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for sortedcontainers 1.5.9
File Interpreter ABI Platform
sortedcontainers-1.5.9-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.0 MB

Release files / sortedcontainers-1.5.9.tar.gz

Download URL sortedcontainers-1.5.9.tar.gz
Size 12.0 MB
Tags Source
SHA-256 checksum
How to use checksums
844daced0f20d75c02ce53f373d048ea2e401ad8a7b3a4c43b2aa544b569efb3
BLAKE2b-256 checksum
How to use checksums
64c8709e55ef31b56ab6920d8680e27812ddfc0b2be76502aad39ad9f5f507e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sortedcontainers-1.5.9-py2.py3-none-any.whl

Download URL sortedcontainers-1.5.9-py2.py3-none-any.whl
Size 31.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
fb9e22cd6ee4b459f0d7b9b4189b19631031c72ac05715563139162014c13672
BLAKE2b-256 checksum
How to use checksums
ea67c76c354ff30a689aeb2c75c4d383ae618c27fc2180d313f387f8918a3429
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

2.4.0

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.5.10

2 release files

This release

1.5.9 This release

2 release files

1.5.7

2 release files

1.5.6

2 release files

1.5.5

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.4.4

1 release file

1.4.3

1 release file

1.4.2

1 release file

0.9.6

1 release file

0.9.5

1 release file

0.9.4

1 release file

0.9.3

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9.0

1 release file

0.8.5

1 release file

0.8.4

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.0

1 release file

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