How to compare two text lists
Compare two text lists while ignoring spacing, case and Unicode formatting differences: what normalization changes, which method fits, and how to check results.
Comparing two lists usually means answering three questions: which items appear in both, which appear only in the first, and which appear only in the second. The hard part is deciding when two lines count as the same item. "Apple" with a trailing space, "apple" in lowercase, and "𝐀𝐩𝐩𝐥𝐞" in styled Unicode letters may look like one item to a person, but a plain character-by-character comparison treats them as three different strings. This guide explains what ignoring formatting differences actually changes, how to choose a comparison method, and how to check that the results hold up where you use them.
What a list comparison checks, and what normalization changes
By default, software compares stored characters, not appearance. Two lines match only if they contain the same sequence of Unicode code points. Ignoring formatting differences means building a comparison key for each line, a cleaned-up version used only for matching, so that differences you don't care about disappear before the lines are compared.
Formatting differences that commonly cause false mismatches include:
- Leading and trailing spaces or tabs, often picked up when copying from spreadsheets or web pages.
- Repeated internal spaces, such as "New York" versus "New York".
- Letter case, such as "BANANA" versus "banana".
- Line endings: Windows-style CRLF versus Unix-style LF, which can leave an invisible carriage return at the end of each line.
- Accented letters stored two ways: "é" as a single precomposed character, or as "e" followed by a combining accent. Both look identical.
- Compatibility characters: non-breaking spaces (U+00A0), fullwidth letters, and styled mathematical letters such as bold or italic Unicode alphabets.
- Invisible format characters: zero-width spaces, byte order marks, and left-to-right or right-to-left marks.
- Duplicates and ordering, which matter for some comparisons and not for others.
Normalization changes only the key, not the meaning of your data, as long as you keep the original lines for the output. Every rule you add also makes the comparison more forgiving, so each rule is a decision about what counts as "the same". Case folding, for example, treats German "ß" and "ss" as equal, which is right for some lists and wrong for others, such as usernames or product codes where case or spelling is significant.
Styled, reversed and mirrored text in a list
Lists copied from social media bios, design mockups or text generators can contain transformed text. Terms like "mirror text" or "fancy font" describe how text looks, not how it is stored, and the storage is what decides whether a comparison matches. The distinctions that matter:
- Character-order reversal: the stored characters are rearranged, so "olleh" is a different string from "hello". No standard normalization undoes this; you would have to reverse it back deliberately before comparing.
- Visual mirroring with CSS: a style such as a horizontal flip changes only how the browser draws the text. The stored characters are unchanged, so if you get the underlying text, it compares as normal text. What actually lands on the clipboard can depend on the browser and the page; this guide has not tested specific cases.
- Unicode character substitution: "fancy" letters are separate characters with their own code points. Unicode compatibility normalization (NFKC) maps many of them back to plain letters, including mathematical bold and italic alphabets and fullwidth forms. It does not map upside-down lookalikes or letters borrowed from other scripts, such as a Cyrillic "а" standing in for a Latin "a"; those stay different and will not match.
- CSS presentation changes: a rule that displays text in uppercase leaves the stored case as typed. Two items can look identical on screen and still differ in storage, which case folding resolves.
- Bidirectional display: right-to-left text such as Arabic or Hebrew is stored in logical order and displayed in visual order, so comparisons work on the stored order. Invisible directional marks (U+200E, U+200F) inserted to control display are real characters and will break a match unless removed.
Which comparison method suits your lists
The first choice is whether order matters. A line-by-line diff treats the lists as documents and reports insertions, deletions and changes in sequence, which suits two versions of the same list. A set comparison ignores order and reports membership only, which suits two independent lists, such as a sign-up export and an attendance list. The second choice is how much control you need over normalization.
| Method | Order matters | Formatting control | What you get | Suits |
| Line-by-line diff (editor or diff utility) | Yes | Usually whitespace and sometimes case options; varies by tool | Changes shown in place | Two versions of the same list |
| Sorted set comparison (sort and comm) | No | Whatever cleanup you apply first | Three result lists of cleaned lines | Quick membership checks on the command line |
| Spreadsheet lookup | No | Helper columns with functions such as TRIM and LOWER | A flag next to each original row | Keeping row context; no scripting |
| Script with explicit normalization | Your choice | Full: Unicode normalization, invisible characters, case | Original lines grouped by result, reproducible | Large, messy or recurring comparisons |
| Online comparison tool | Varies | Varies; check the tool's documentation | Convenient on-screen results | Small, non-sensitive lists |
Duplicates also need a decision. A set comparison collapses repeated items, so "apple" listed three times in one list and once in the other counts as a match. If counts matter, for example inventory lines, compare item counts rather than presence.
Illustrative matched input
The pairs below show which differences each level of normalization resolves. The results follow from Unicode normalization and case-folding rules; they are illustrative, not output observed from a particular tool.
| List A item | List B item | Difference | Exact match | Trim + case fold | Plus NFKC and format-character removal |
| Banana | " BANANA " | Spaces and case | No | Yes | Yes |
| Café (precomposed é) | Café (e + combining accent) | Accent storage | No | No | Yes |
| Cherry | 𝐂𝐡𝐞𝐫𝐫𝐲 (mathematical bold) | Styled Unicode letters | No | No | Yes |
| Kiwi | Kiwi with a zero-width space inside | Invisible character | No | No | Yes |
| banana | ɐuɐuɐq (upside-down lookalikes) | Character substitution and reversal | No | No | No |
Command-line set comparison
On Linux or macOS, sort and comm compare two lists without regard to order. comm requires both inputs to be sorted the same way, so set the same locale for both commands. The cleanup below removes carriage returns, trims spaces, drops blank lines, lowercases and removes duplicates. It is ASCII-oriented: it does not apply Unicode normalization, and the carriage-return pattern assumes GNU sed. The results show cleaned lines, not the originals.
# Clean each list: strip CR, trim, drop blanks, lowercase, dedupe
for f in list_a.txt list_b.txt; do
sed -e 's/\r$//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e '/^$/d' "$f" \
| tr '[:upper:]' '[:lower:]' \
| LC_ALL=C sort -u > "${f%.txt}.clean"
done
LC_ALL=C comm -12 list_a.clean list_b.clean # in both
LC_ALL=C comm -23 list_a.clean list_b.clean # only in A
LC_ALL=C comm -13 list_a.clean list_b.clean # only in B
Spreadsheet comparison
Put the lists in columns A and B, then build cleaned helper columns so the original rows stay visible. For example, C2 contains =LOWER(TRIM(A2)) and D2 contains =LOWER(TRIM(B2)), filled down. Then E2 can flag each list A item with =IF(COUNTIF(D:D, C2)>0, "In both", "Only in A"), and a matching formula in the other direction flags list B. Two caveats: in Excel, COUNTIF treats *, ? and ~ as wildcard characters, so items containing them can match unintended rows, and TRIM is documented to remove only the ordinary space, not the non-breaking space. Check your spreadsheet application's documentation for its own lookup and trimming behavior, and use SUBSTITUTE or an exact-equality formula where those characters appear.
Script with explicit normalization
A short Python script gives you full control and keeps the original line for each match. It applies NFKC normalization, removes invisible format characters (Unicode category Cf), collapses whitespace and case-folds. Removing all Cf characters also strips zero-width joiners, which are part of some emoji sequences, so remove that line if your lists contain emoji that must stay distinct. The code is offered as a starting point and has not been run against your data.
import unicodedata
def key(line):
text = unicodedata.normalize("NFKC", line)
text = "".join(ch for ch in text if unicodedata.category(ch) != "Cf")
text = " ".join(text.split())
return text.casefold()
def load(path):
items = {}
with open(path, encoding="utf-8") as f:
for line in f:
if line.strip():
items.setdefault(key(line), line.rstrip("\r\n"))
return items
a = load("list_a.txt")
b = load("list_b.txt")
for label, keys, source in (
("In both", a.keys() & b.keys(), a),
("Only in A", a.keys() - b.keys(), a),
("Only in B", b.keys() - a.keys(), b),
):
print(f"{label}:")
for k in sorted(keys):
print(" " + source[k])
Will the results stay readable and usable?
Most comparison problems start at the clipboard. Copying from web pages, PDFs and spreadsheets can carry non-breaking spaces, tabs, carriage returns and invisible marks into your lists, and pasting into a rich-text field may add or alter characters again. Before trusting a result, paste a mismatched pair into a plain-text editor that can display invisible characters, or inspect the code points. If two lines that look identical still fail to match, a hidden character is the likeliest cause.
Report the original lines rather than the normalized keys, so readers see what is actually in the data and can fix the source. If you share results, note which rules were applied, such as "case and spacing ignored", because a match under lenient rules is not proof that two records are byte-for-byte identical.
Styled Unicode letters and lookalike characters also affect readability beyond the comparison. They are distinct characters with names such as "MATHEMATICAL BOLD CAPITAL C", and how a screen reader voices them, or whether a font displays them at all, depends on the software, settings and fonts involved; this guide has not tested specific combinations. For lists that people will read, search or process further, converting styled text to plain letters is the safer default.
Online comparison tools receive whatever you paste, and that data may be logged or retained depending on the service. Lists of names, email addresses, customer IDs or other personal or confidential data are better compared locally with a spreadsheet, the command line or a script. If you do use an online tool, read its privacy terms first and consider removing or masking sensitive fields.
Gaps to check in your own setup
No tool-specific test results were available for this guide. The comparison tables are illustrative and based on Unicode rules, not on observed output from a particular application. Clipboard behavior for CSS-styled or mirrored text, screen-reader handling of styled characters, and the exact normalization options of individual diff and comparison tools vary and should be confirmed in your environment, ideally by running a few known matched and mismatched pairs through your chosen method before relying on it for the full lists.