pygtrie: Pure Python Trie Data Structure
pygtrie is a pure Python library implementing a trie data structure. It provides `Trie`, `CharTrie`, and `StringTrie` classes, each implementing a mutable mapping (dictionary-like) interface. Its strengths lie in prefix-based operations, such as iterating over or deleting subtries, prefix checking, and shortest/longest prefix look-ups. It also includes a `PrefixSet` for managing sets of prefixes. The current version is 2.5.0, with releases occurring periodically to introduce features and address compatibility.
Warnings
- breaking The module name was changed from `trie` to `pygtrie` in the 1.0 release. Existing code using `from pytrie import trie` will break.
- breaking Prior to version 2.0, child nodes were sorted by default during iteration and traversal. Since 2.0, this behavior is disabled for performance. Iteration order is no longer guaranteed unless explicitly enabled.
- deprecated The objects returned by `Trie.shortest_prefix()` and `Trie.longest_prefix()` could previously be treated directly as `(key, value)` tuples. This is deprecated.
- gotcha When using the generic `pygtrie.Trie` with string keys, `iterkeys()` and similar methods will return keys as tuples of characters (e.g., `('f', 'o', 'o')`) instead of strings. This can be unexpected.
- gotcha The `PrefixSet.add()` method has counter-intuitive behavior: adding a key that is a prefix of existing keys will remove those longer keys. Also, adding a key that already has a prefix in the set will have no effect.
Install
-
pip install pygtrie
Imports
- Trie
from pygtrie import Trie
- CharTrie
from pygtrie import CharTrie
- StringTrie
from pygtrie import StringTrie
- PrefixSet
from pygtrie import PrefixSet
Quickstart
from pygtrie import Trie, StringTrie
t = Trie()
t['foo'] = 1
t['bar'] = 2
t['baz'] = 3
t['foobar'] = 4
print(f"Trie contains 'foo': {t['foo']}")
print(f"All items with prefix 'foo': {list(t.items('foo'))}")
s_trie = StringTrie(separator='/')
s_trie['/home/user/docs/report.pdf'] = 'Report 2023'
s_trie['/home/user/photos/vacation.jpg'] = 'Vacation Pics'
print(f"StringTrie for '/home/user/docs': {list(s_trie.keys('/home/user/docs'))}")