editdistpy
editdistpy (version 0.2.0) provides fast implementations of the Levenshtein and Damerau Optimal String Alignment (OSA) edit distance algorithms. Written in Cython/C++, it offers significant speed improvements for string comparison tasks. The library is actively maintained, with releases made periodically, the latest major version (0.2.0) released in February 2026.
Warnings
- breaking Support for Python 3.8 was officially dropped in version 0.1.6. Users on Python 3.8 or older must upgrade their Python environment to Python 3.9 or newer to use versions >= 0.1.6.
- gotcha When `max_distance` is provided to the `distance` method, it will return `-1` if the calculated edit distance exceeds this maximum. This is an intentional performance feature, not an error. To always get the full distance, pass `sys.maxsize` as `max_distance`.
- gotcha The `damerau_osa` module implements the Damerau Optimal String Alignment (OSA) distance, which is a restricted form of the Damerau-Levenshtein distance. OSA imposes a constraint that no substring may be edited more than once, meaning it does not always satisfy the triangle inequality and might return a different distance in certain cases compared to a full Damerau-Levenshtein implementation.
- deprecated In versions prior to 0.1.3, the library might not correctly handle distance calculations when one of the input strings is `None` or when `max_distance` is set to `0`. These issues were fixed in version 0.1.3.
Install
-
pip install editdistpy
Imports
- levenshtein
from editdistpy import levenshtein
- damerau_osa
from editdistpy import damerau_osa
Quickstart
import sys
from editdistpy import levenshtein, damerau_osa
string_1 = "flintstone"
string_2 = "hanson"
# Levenshtein distance
max_dist_lev = 2
lev_dist_bounded = levenshtein.distance(string_1, string_2, max_dist_lev)
print(f"Levenshtein (max_distance={max_dist_lev}): {lev_dist_bounded}") # Expected: -1
lev_dist_full = levenshtein.distance(string_1, string_2, sys.maxsize)
print(f"Levenshtein (full): {lev_dist_full}") # Expected: 6
# Damerau Optimal String Alignment (OSA) distance
max_dist_osa = 2
osa_dist_bounded = damerau_osa.distance(string_1, string_2, max_dist_osa)
print(f"Damerau OSA (max_distance={max_dist_osa}): {osa_dist_bounded}") # Expected: -1
osa_dist_full = damerau_osa.distance(string_1, string_2, sys.maxsize)
print(f"Damerau OSA (full): {osa_dist_full}") # Expected: 6