all Python 3difflibfunction
difflib.get_close_matches()
sequence matching
Returns the options from a list that are most similar to a word, the basis of "did you mean..." hints.
Syntax
difflib.get_close_matches(word, possibilities, n=3, cutoff=0.6)
The comparison is case-sensitive: 'APPLE' will not find 'apple', so lower() both sides. The cutoff=0.6 threshold drops anything less similar, and with no candidates you get an empty list, not None; n=3 caps the number of answers. The result is ordered from best to worst match, not by the original list.
Examples
import difflib
commands = ['start', 'stop', 'status', 'restart']
print(difflib.get_close_matches('stat', commands)) # → ['start', 'status', 'restart']
print(difflib.get_close_matches('lenght', ['len', 'length', 'list'], n=1)) # → ['length']
import difflib
print(difflib.get_close_matches('APPLE', ['apple'])) # → []
print(difflib.get_close_matches('APPLE'.lower(), ['apple'])) # → ['apple']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy