intervaltree
Editable interval tree data structure for Python 2 and 3
What it is and what it does
intervaltree is a mutable, self-balancing interval tree for Python that stores ranges (intervals) and allows efficient queries by point, by range overlap, or by range envelopment. It was designed to support tagging text and time intervals, where intervals include the lower bound but exclude the upper bound. The package provides a rich API for insertion, deletion, and querying, as well as set-like operations (union, difference, intersection) and restructuring methods (merge, chop, slice).
The tree automatically maintains balance using AVL rebalancing, so you can add and remove intervals without manually managing the structure. It depends only on sortedcontainers for its underlying sorted storage, keeping the dependency footprint minimal. The library supports both Python 2.7 and modern Python versions (3.5 through 3.14), making it suitable for legacy and current codebases alike.
Use it for:
- Tag text spans or time intervals in documents or logs, then query which tags overlap a given point or range.
- Store scheduling or calendar events and find all events that overlap or are contained within a time window.
- Manage genomic or biological sequence annotations where intervals represent features and you need to query overlaps.
- Index spatial or temporal data where you need fast overlap detection between ranges without manual tree balancing.
- Implement interval-based caching or memoization where you store results for ranges and retrieve them by overlap.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A self-balancing interval tree data structure that stores and queries overlapping or enveloped ranges, supporting point lookups, range overlaps, and range envelopment queries.
Yes. intervaltree is a stable, well-maintained library with low install friction, permissive licensing, no known vulnerabilities, and a focused, proven API for a common data structure problem. Install it if you need to store and query overlapping or enveloped ranges; the self-balancing design and rich query interface make it significantly easier than building your own tree.
Install
intervaltree on PyPI
pip
pip install intervaltreeuv
uv add intervaltreepoetry
poetry add intervaltreeInstalling intervaltree
Before you install
Low install friction with a single pure-Python runtime dependency (sortedcontainers). The package is in production-stable status and has been actively maintained, with the latest release in 2025 and repository last commit in 2025. Supports Python 2.7 and 3.5 through 3.14.
License in practice
Licensed under Apache-2.0 (permissive), allowing commercial use, modification, and distribution with minimal restrictions—suitable for most projects.
Quickstart
pip install intervaltree
from intervaltree import Interval, IntervalTree
t = IntervalTree()
t[1:5] = "data"
result = sorted(t.overlap(2, 4))
print(result)
Verify before relying
- Whether the package's AVL balancing guarantees specific query performance characteristics (e.g., O(log n) lookups) under typical usage patterns.
- Real-world performance comparison with alternative interval tree implementations for large datasets.
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,>=2.7.18) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — sortedcontainers |
| Maintenance | aging — 233 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 6,271,448/month — #1,942 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: intervaltree-3.2.1-py2.py3-none-any.whl
Keywords: data-structure, interval-tree, intervals, tree
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
nclsNCLS provides a fast interval overlap query…
permissive · top 15,000 on PyPI
red-black-tree-modProvides Python implementations of red-black…
permissive · top 5,000 on PyPI
portionportion provides data structures and operations…
copyleft · top 5,000 on PyPI
intervalsProvides Python classes for representing and…
permissive · top 15,000 on PyPI
aptedComputes the tree edit distance between two…
permissive · top 15,000 on PyPI
igwn-segmentsProvides segment, segmentlist, and…
copyleft · top 15,000 on PyPI
pyrangesPyRanges provides efficient querying and…
permissive · top 15,000 on PyPI
adjustTextAutomatically repositions text labels on…
permissive · top 5,000 on PyPI
MAPIEMAPIE computes prediction intervals and…
permissive · top 15,000 on PyPI
rcsliceSlices lists using a row-column notation with…
permissive · top 15,000 on PyPI