About Approximate Dictionary Matching

You can match dictionary keys approximately using the thefuzz library:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> from thefuzz import process

>>> FRUITS = {"apples": "🍎", "bananas": "🍌", "cherries": "🍒"}

>>> def get_approximate_value(d, key):
...    corrected_key, preciseness = process.extractOne(
...        query=key, choices=d.keys()
...    )
...    return d[corrected_key]

>>> get_approximate_value(FRUITS, "CHERRY")
'🍒'

Install thefuzz and its dependency for the speed by pip as follows:

1
2
(venv)$ pip install thefuzz
(venv)$ pip install python-Levenshtein

Tips and Tricks Programming Python 3