Skip to main content

Python Sorted Container Types: SortedList, SortedDict, and SortedSet

Project description

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

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

Python’s standard library is great until you need a sorted container 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.

Things shouldn’t be this way. Not in 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 at home without at least a gigabyte of memory. In Python an integer requires at least 12 bytes. SortedList will add about 4 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 for which every node must also store two pointers to children nodes.

SortedContainers takes all of the work out of Python sorted types - 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.6, 2.7, 3.2, 3.3, 3.4 and PyPy 2.2+, PyPy3 2.3.1+

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.

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 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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sortedcontainers-0.9.4.tar.gz (31.2 kB view details)

Uploaded Source

File details

Details for the file sortedcontainers-0.9.4.tar.gz.

File metadata

File hashes

Hashes for sortedcontainers-0.9.4.tar.gz
Algorithm Hash digest
SHA256 c5775ab80486e48b670c94c23652cff9f08c0b6dc00a9a36dfd9438eaa6714f3
MD5 06fe292633d6352d7460cda5724c80f4
BLAKE2b-256 a952db030b5556101938076b0d52d5c7bec4aa23b9164e288cff4a540c942985

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page