Skip to content
Compare repeated records with SequenceMatcher autojunk disabled

Compare repeated records with SequenceMatcher autojunk disabled

November 24, 2026

For long sequences of repeated records, SequenceMatcher can treat common elements as unsuitable matching anchors. Compare the default result with autojunk=False when repeated values are meaningful in your data.

Run the example

Tested on Linux with Python 3.12.14. No third-party packages or network access are needed. In one Bash session, create a temporary working directory:

demo_start="$PWD"
demo_dir="$(mktemp -d)" || exit 1
cd -- "$demo_dir" || exit 1

Save as demo_repeated_records.py in that directory. Use this filename to avoid shadowing a standard-library module.

from difflib import SequenceMatcher

before = ["old"] + ["same"] * 300
after = ["new"] + ["same"] * 300
for automatic in (True, False):
    matcher = SequenceMatcher(None, before, after, autojunk=automatic)
    print(f"autojunk={automatic}: ratio={matcher.ratio():.3f}")
    print("Matching records:", sum(block.size for block in matcher.get_matching_blocks()))
print("Exactly equal:", before == after)

Run it:

python3 demo_repeated_records.py

Captured output:

autojunk=True: ratio=0.000
Matching records: 0
autojunk=False: ratio=0.997
Matching records: 300
Exactly equal: False

Interpret the result

The automatic heuristic applies when the second sequence has at least 200 elements. An element whose duplicates exceed 1% of that sequence is considered popular. Disabling it lets this fixture’s repeated records anchor the match. This does not make the result a minimal edit script, a semantic comparison, or an equality test. It can also increase work substantially on repetitive inputs; benchmark representative sizes before applying it to large datasets. The final equality check remains false because the first record differs.

See the version-specific Python reference for the API contract.

Clean up

In the same Bash session, remove the source file and then the empty working directory:

rm -- demo_repeated_records.py
cd -- "$demo_start" || exit 1
rmdir -- "$demo_dir"
unset demo_start demo_dir

If the directory is not empty, inspect its remaining contents before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on