DEV Community

Kelvin Wangonya
Kelvin Wangonya

Posted on • Originally published at wangonya.com on

8 3

difflib - Finding close matches of strings from a list

Say we have a list of strings: _list = [...,] and user input _input = '...', how do we find the items in _list that most closely resemble _input?

Python has a built-in package called difflib with the function get_close_matches() that can help us.

get_close_matches(word, possibilities, n, cutoff) accepts four parameters:

  • word - the word to find close matches for in our list
  • possibilities - the list in which to search for close matches of word
  • n (optional) - the maximum number of close matches to return. Must be > 0. Default is 3.
  • cutoff (optional) - a float in the range [0, 1] that a possibility must score in order to be considered similar to word. 0 is very lenient, 1 is very strict. Default is 0.6.

An example from the docs:

Python 3.7.3

>>> from difflib import get_close_matches
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
Enter fullscreen mode Exit fullscreen mode

The above example can easily be modified to use a custom list _list for possibilities and user input _input for word.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (2)

Collapse
 
mauroscianca98 profile image
mascIT

I'mma add this 100% on my SmartCoding app.

Collapse
 
wangonya profile image
Kelvin Wangonya

Awesome!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay