skillfed

pygtrie

A pure Python trie data structure implementation.

pygtrie v2.5.0 13.9M downloads/30d#1,261 on PyPI42
Permissive license Apache-2.0 DORMANT released

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 pygtrie

uv

uv add pygtrie

poetry

poetry add pygtrie

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: Apache Software LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 2Programming Language :: Python :: 2.7Programming Language :: Python :: 3Topic :: Software Development :: Libraries :: Python Modules

Tags

trie data structure pythonprefix tree implementationradix tree libraryprefix lookup dictionarystring prefix matchingtrie prefix setpython mutable mapping trie
data-structuresprefix-matching

More Python Modules packages