Polyleven
Polyleven is a hyper-fast Python library for computing Levenshtein distance, implemented in C for optimal performance. It is designed to be efficient for comparing both short and long string inputs, is stand-alone with no external Python dependencies, and is distributed under the MIT License. The current version is 0.11.0, released on February 9, 2026, indicating an active release cadence.
Warnings
- gotcha For optimal performance, especially with long strings or when only interested in small distances, provide an integer `k` as the third argument to the `levenshtein()` function. This `max_threshold` limits the maximum distance computed, making the operation significantly more efficient. If the true distance exceeds `k`, the function returns `k + 1`.
- gotcha The license for Polyleven changed from Public Domain to MIT License starting from version 0.7. Users of older versions relying on Public Domain terms should be aware of this change.
- gotcha While the `max_threshold` argument generally improves performance, the speed boost is most noticeable when the threshold `k` is small, typically less than 3. Larger thresholds still offer some benefit but diminish as `k` approaches the full string length.
Install
-
pip install polyleven
Imports
- levenshtein
from polyleven import levenshtein
Quickstart
from polyleven import levenshtein
# Calculate Levenshtein distance between two strings
distance1 = levenshtein('kitten', 'sitting')
print(f"Distance between 'kitten' and 'sitting': {distance1}")
# Calculate Levenshtein distance with a maximum threshold for efficiency
# If the actual distance exceeds the threshold, the threshold + 1 is returned.
distance2 = levenshtein('apple', 'aple', 1) # Actual distance is 1
print(f"Distance between 'apple' and 'aple' with max_threshold=1: {distance2}")
distance3 = levenshtein('banana', 'orange', 2) # Actual distance is higher than 2
print(f"Distance between 'banana' and 'orange' with max_threshold=2: {distance3}")
assert distance1 == 3
assert distance2 == 1
assert distance3 == 3 # Returns threshold + 1 because actual distance > threshold