{"id":1542,"library":"marisa-trie","title":"MARISA Trie for Python","description":"marisa-trie provides static, memory-efficient, and fast Trie-like data structures for Python, wrapping the C++ MARISA library. It's ideal for use cases like autocomplete, spell checkers, and storing large sets of strings or byte sequences for fast prefix matching and lookup. The library is actively maintained with regular updates to support new Python versions and minor feature enhancements.","status":"active","version":"1.4.1","language":"en","source_language":"en","source_url":"https://github.com/pytries/marisa-trie","tags":["trie","data-structure","prefix-search","autocomplete","memory-efficient","static"],"install":[{"cmd":"pip install marisa-trie","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Trie is an alias for BytesTrie and expects byte strings.","symbol":"Trie","correct":"from marisa_trie import Trie"},{"note":"The primary Trie implementation for byte strings.","symbol":"BytesTrie","correct":"from marisa_trie import BytesTrie"},{"note":"For storing structured data (records) associated with keys.","symbol":"RecordTrie","correct":"from marisa_trie import RecordTrie"},{"note":"Introduced in v1.4.0, specifically for string-to-string mappings without manual encoding.","symbol":"StringTrie","correct":"from marisa_trie import StringTrie"}],"quickstart":{"code":"from marisa_trie import BytesTrie, StringTrie\n\n# Example with BytesTrie (most common, requires bytes)\nwords_bytes = [b'apple', b'apricot', b'banana', b'bandana', b'cherry']\nb_trie = BytesTrie(words_bytes)\n\nprint(f\"'apple' in BytesTrie: {b'apple' in b_trie}\")\nprint(f\"Words starting with 'ap': {list(b_trie.keys(b'ap'))}\")\n\n# Example with StringTrie (requires strings, new in 1.4.0)\nwords_str = ['hello', 'world', 'helium', 'wonder']\ns_trie = StringTrie(words_str)\n\nprint(f\"'hello' in StringTrie: {'hello' in s_trie}\")\nprint(f\"Words starting with 'h': {list(s_trie.keys('h'))}\")\n","lang":"python","description":"Demonstrates creating a BytesTrie (the default Trie) and a StringTrie, then performing basic lookups and prefix searches. Note that BytesTrie expects byte strings for all operations."},"warnings":[{"fix":"Upgrade to Python 3.9 or newer. For Python 3.7/3.8, use marisa-trie<1.3.0.","message":"Python 3.7 and 3.8 support was dropped. Users on these versions must upgrade Python or stick to older marisa-trie versions.","severity":"breaking","affected_versions":">=1.3.0"},{"fix":"Plan your data set in advance. If dynamic updates are needed, MARISA Trie might not be the right choice, or you'll need to re-build the trie periodically.","message":"MARISA tries are immutable after creation. You cannot add or remove keys once the trie is built. To modify, you must build a new trie.","severity":"gotcha","affected_versions":"all"},{"fix":"Encode strings to bytes (e.g., `s.encode('utf-8')`) before passing them to `BytesTrie` methods. Alternatively, use `StringTrie` (available since v1.4.0) if you primarily work with `str` type keys and values.","message":"The default `Trie` and `BytesTrie` classes expect `bytes` objects, not `str`. Operations like `trie.keys(b'prefix')` or `b'word' in trie` require byte strings.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}