Python - find all similar strings from a provided word within a list of strings


Python tip:

You can use get_close_matches from difflib to find all similar strings from a provided word within a list of strings:

difflib.get_close_matches(word, possibilities, n=3, cutoff=0.6)

For example, find similar names👇

from difflib import get_close_matches

name = "John"
names = ["John", "Jane", "Bob", "Daisy", "Jim", "Mark", "Johnny", "Mike"]


print(get_close_matches(name, names))
# => ['John', 'Johnny']