Polyleven

0.11.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to import the `levenshtein` function and use it to calculate the Levenshtein distance between two strings. It also shows how to leverage the optional `max_threshold` argument to improve performance, especially when only interested in distances below a certain value.

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

view raw JSON →