pygtrie
A pure Python trie data structure implementation.
What it is and what it does
pygtrie is a pure Python trie (prefix tree) library that stores key-value pairs organized by shared prefixes, allowing efficient prefix-based lookups and iteration. It provides three main classes—Trie, CharTrie, and StringTrie—each implementing Python's mutable mapping interface (dict-like behavior), plus a PrefixSet class for storing sets of prefixes where membership includes all keys sharing a stored prefix.
The library is useful when you need to query or iterate over all keys with a common prefix, find shortest or longest prefix matches, or delete entire subtrees of keys at once. It works as a drop-in dict replacement in most cases but shines when prefix operations are central to your use case. The package has no runtime dependencies and is compatible with Python 2.x and 3.x, though it is currently dormant—last release was 2022-07-16.
Use it for:
- Autocomplete and search-as-you-type systems that need to retrieve all keys starting with a user's input prefix
- IP routing tables or CIDR block lookups where you need longest-prefix matching
- Dictionary or spell-checker implementations that support prefix-based word lookups and suggestions
- File path or URL hierarchies where you need to query or delete all entries under a given path prefix
- Storing and querying hierarchical configuration keys or namespaced settings by prefix
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
pygtrie provides pure Python implementations of trie (prefix tree) data structures—Trie, CharTrie, StringTrie, and PrefixSet—that implement the mutable mapping interface for efficient prefix-based lookups and storage.
Yes, if you need trie-based prefix lookups and can tolerate dormant maintenance. The package is stable, has no dependencies, and solves a specific problem well. However, verify that the empty requires_python field doesn't mask compatibility issues with your Python version, and be aware that bug fixes or updates are unlikely without community contribution.
Install
pygtrie on PyPI
pip
pip install pygtrieuv
uv add pygtriepoetry
poetry add pygtrieInstalling pygtrie
Before you install
Installation is straightforward with no runtime dependencies. The package is marked Production/Stable but dormant—last release was 2022-07-16 and the repository received its last commit 2024-03-26, so expect no active maintenance or rapid bug fixes.
License in practice
Licensed under Apache-2.0 (permissive), so you can use, modify, and distribute pygtrie freely in commercial and private projects with minimal restrictions.
Quickstart
pip install pygtrie
from pygtrie import StringTrie
trie = StringTrie({'foo/bar': 42}, separator='/')
print(trie['foo/bar']) # 42
print(list(trie.prefixes('foo/bar/baz'))) # prefix lookup
Verify before relying
- Whether the package is actively maintained or receives security updates despite dormant status
- Performance characteristics and memory usage compared to alternative trie implementations
- Compatibility with Python versions beyond 2.7 and 3.x (requires_python field is empty)
Package facts
| License | Apache-2.0 (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | dormant — 1,490 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 13,875,383/month — #1,261 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pygtrie-2.5.0-py3-none-any.whl
Keywords: trie, prefix tree, data structure
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
marisa-trieProvides memory-efficient trie data structures…
copyleft · top 5,000 on PyPI
PyTriePyTrie provides a pure Python 3 trie data…
permissive · top 15,000 on PyPI
sqltrieSQLTrie implements a prefix tree (trie) data…
permissive · top 5,000 on PyPI
trieImplements the Ethereum Merkle Trie data…
permissive · top 15,000 on PyPI
py-radixImplements a radix tree data structure for…
permissive · top 15,000 on PyPI
cidr-trieStores and queries CIDR IP address blocks (IPv4…
permissive · top 15,000 on PyPI
retrieBuilds efficient Trie-based regex patterns for…
permissive · top 15,000 on PyPI
treelibProvides a simple tree data structure…
permissive · top 5,000 on PyPI
immutablesProvides an immutable mapping type backed by a…
permissive · top 5,000 on PyPI
multi_key_dictA dictionary that maps multiple keys to the…
permissive · top 5,000 on PyPI