Skip to main content

SortedContainers

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.

Features

  • Pure-Python

  • Fully documented

  • Benchmark comparison

  • 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 mostly a Python set with a SortedList index)

  • Developed on Python 2.7

  • Tested on Python 2.6, 2.7, 3.2, 3.3, and 3.4

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

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.

Metadata

Release files for sortedcontainers 0.8.4

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 0.8.4
File Size Uploaded Provenance Actions
sortedcontainers-0.8.4.tar.gz 18.4 kB Without provenance Details for sortedcontainers-0.8.4.tar.gz

Release files / sortedcontainers-0.8.4.tar.gz

Download URL sortedcontainers-0.8.4.tar.gz
Size 18.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8f3bb2961a2caae89635e8225306e0b8ba43381d3ae49d6cda342c5fd5819097
BLAKE2b-256 checksum
How to use checksums
dd8b9f70bf8e4911547045adc4d96e59dd05e559b96b953da50c0d917ef1dbf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for sortedcontainers 0.8.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.8.4 This release

1 release file No provenance

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