Back to Blog
Python

Using Python RapidFuzz extract and extractOne for Fuzzy Matching

python rapidfuzz extract and extractone: Learn how to use RapidFuzz's extract and extractOne functions to find the best matching strings in Python, with parameters, sc...

fuzzy matchingRapidFuzzstring similarityPythontext processing
A Python code snippet showing RapidFuzz extract and extractOne functions comparing strings with similarity scores.

When you need to find the closest matching string from a list of candidates in Python, python rapidfuzz extract and extractone provide the core entry points. These functions from the rapidfuzz.process module let you compare a query against a collection of choices and return ranked matches with similarity scores.

What extract and extractOne Do in RapidFuzz

rapidfuzz.process.extract and rapidfuzz.process.extractOne are the two primary functions for comparing a query string against a collection of candidate strings. They both use a scorer (by default fuzz.ratio) to compute similarity scores and return the best matches.

The key difference is the shape of the result. extractOne returns a single tuple (choice, score, index) for the highest-scoring match, or None if no match exceeds the score_cutoff. extract returns a list of such tuples, sorted by score in descending order, and lets you control how many results to return with the limit parameter.

Both functions accept the same core arguments: query, choices, scorer, processor, and score_cutoff. The processor argument applies a callable to both the query and each choice before scoring, which is useful for normalization like lowercasing or stripping whitespace.

extractOne: When You Need a Single Best Match

Use extractOne when you only care about the closest match. For example, matching a user-typed product name against a catalog and returning the canonical name.

from rapidfuzz import process, fuzz catalog = ["Wireless Mouse", "USB-C Cable", "Mechanical Keyboard"] query = "wireless mous" best = process.extractOne(query, catalog, scorer=fuzz.WRatio) print(best) # ('Wireless Mouse', 90.0, 0)

Here fuzz.WRatio is case-insensitive and handles partial matches well. The returned tuple contains the matched choice, the similarity score (0–100), and the index in the original list.

If no candidate scores above the default cutoff (0), extractOne returns None. You can raise the cutoff to avoid weak matches:

best = process.extractOne(query, catalog, scorer=fuzz.ratio, score_cutoff=80) if best: print(best) else: print("No match above 80")

Because extractOne only needs the top result, it can stop early when a perfect score is found, making it faster than extract for large candidate sets.

extract: Getting Multiple Matches with Scores

When you need a ranked list of plausible matches—for example, showing autocomplete suggestions—use extract. It returns a list of tuples, each with the same structure as extractOne's result.

from rapidfuzz import process, fuzz choices = ["apple pie", "apple juice", "banana bread", "apple cider"] query = "apple" matches = process.extract(query, choices, scorer=fuzz.partial_ratio, limit=3) for choice, score, index in matches: print(f"{choice}: {score}")

The limit parameter caps the number of returned matches. Without it, all choices are returned, sorted by score. If you only need the top few, setting a limit reduces memory and processing time.

You can also filter with score_cutoff to exclude low-scoring entries. This is useful when you want to show only matches above a certain confidence threshold.

Choosing the Right Scorer for Your Data

The scorer determines how similarity is measured. RapidFuzz provides several, each suited to different data shapes:

  • fuzz.ratio – standard Levenshtein-based similarity, case-sensitive.
  • fuzz.partial_ratio – best matching substring, good for long strings with common prefixes.
  • fuzz.token_sort_ratio – sorts tokens before comparing, useful for reordered words.
  • fuzz.token_set_ratio – ignores duplicate tokens and compares sets, robust for extra words.

For example, matching "New York" against "York New" would score low with ratio but high with token_sort_ratio. Choose the scorer based on the expected variation in your data.

Using limit and score_cutoff to Control Results

Both extract and extractOne accept score_cutoff. For extract, limit is also available. These parameters let you balance recall and precision.

matches = process.extract( query, choices, scorer=fuzz.ratio, limit=5, score_cutoff=70 )

Setting limit to a small number reduces the size of the returned list, which matters when choices contains thousands of entries. score_cutoff avoids returning irrelevant matches, so you don't have to filter the result afterward.

For extractOne, score_cutoff is the only filter. If you want to enforce a minimum similarity, pass it explicitly; otherwise, the function returns the best match even if its score is low.

Performance and Memory Behavior of extract vs extractOne

RapidFuzz is implemented in C++ and uses optimized algorithms, so both functions are significantly faster than pure-Python alternatives like fuzzywuzzy. However, the choice between extract and extractOne affects runtime.

extractOne can terminate early when it finds a perfect score (100) or when it determines that no remaining candidate can beat the current best. This makes it more efficient for large candidate lists when you only need the top match.

extract with a limit still has to score all candidates to produce a sorted list, unless the scorer supports early termination internally. For very large datasets, consider pre-filtering choices with a cheaper metric (e.g., length difference) before running extract.

Memory usage is proportional to the number of returned matches. With limit set, extract only allocates for that many results. Without a limit, it stores scores for every choice, which can be significant for millions of entries.

Common Mistakes and How to Avoid Them

One frequent mistake is forgetting that extractOne returns None when no match passes the cutoff. Always check for None before unpacking.

Another is assuming the default scorer is case-insensitive. fuzz.ratio is case-sensitive, so "Apple" and "apple" will not match perfectly. Use fuzz.WRatio or apply a processor that lowercases both sides.

process.extractOne(query, choices, processor=lambda s: s.lower())

Also, remember that the processor is applied to both the query and the choices. If you pass a processor that expects a string, ensure your choices are strings. Passing None disables preprocessing.

Finally, be aware that extract returns tuples with the original choice object, not a copy. If you modify the list of choices after calling extract, the returned references still point to the original objects, which may or may not be what you expect.

python rapidfuzz extract and extractone: Practical Usage and | RYUSLOG DEV