You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

prefix-tree

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prefix-tree

A pure Python prefix tree (trie) for fast in-memory prefix search and filtering

0.0.8
Source
pipPyPI
Maintainers
1

PyPI - Downloads PyPI License

📦 prefix_tree

A lightweight, pure-Python prefix tree (trie) implementation for fast in-memory prefix search, autocomplete, and filtering based on metadata. Useful for building autocomplete engines, suggestion systems, and efficient word lookups.

✨ Features

  • In-memory key-value storage using a prefix tree (trie)
  • Fast search by prefix
  • Sortable results by dictionary key
  • Query filtering by field values (exact match)
  • Pure Python, no dependencies
  • Compatible with Python 3.10+

📅 Installation

pip install prefix-tree==0.0.7

🚀 Usage Example

from prefix_tree import Trie

# Create a new trie
trie = Trie()

# Insert words with associated metadata
trie.insert("hello", {"name": "hello", "amount": 10, "gender": "t", "type": "t"})
trie.insert("help", {"name": "help", "amount": 5, "gender": "f", "type": "f"})
trie.insert("hell", {"name": "hell", "amount": 7, "gender": "t", "type": "f"})

# Search by prefix and sort by amount (descending)
results = trie.get_by_prefix_sort_desc_by("hel", "amount")
print(results)
# Output:
# [{'name': 'hello', 'amount': 10, ...}, {'name': 'hell', 'amount': 7, ...}, {'name': 'help', 'amount': 5, ...}]

# Search by prefix and filter by query
filtered = trie.get_by_prefix_and_query("hel", {"gender": "t"})
print(filtered)
# Output:
# [{'name': 'hello', 'amount': 10, ...}, {'name': 'hell', 'amount': 7, ...}]

🔧 Build & Upload to PyPI

python3 -m build
twine upload dist/*

🧠 Use Cases

  • Autocomplete and typeahead suggestions
  • Named entity lookup with filters
  • Efficient in-memory keyword searches
  • Building simple text-based databases
  • autocomplete-full-name (GitHub)

📚 Keywords

autocomplete, trie, prefix search, in-memory database, suggestions, python trie, word search, autocompletion, fast lookup, filtering

📝 License

MIT License (see LICENSE for details)

✨ Author

Created by ilia iakhin

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.