Choose a composite CSV key when one identifier is not unique
Also published in our Blogger archive.
Quick answer
A tuple is an ordered, hashable Python value, which makes it suitable for a declared composite key.
Example
The loop forms (region, id) for every row. The second EU/7 repeats that exact tuple, while US/7 remains distinct even though its ID is the same. The assertion demonstrates the distinction directly.
import csv, io
rows = list(csv.DictReader(io.StringIO('region,id\nEU,7\nUS,7\nEU,7\n')))
seen = set()
duplicates = []
for row in rows:
key = (row['region'], row['id'])
if key in seen:
duplicates.append(key)
seen.add(key)
assert duplicates == [('EU', '7')]
print(duplicates)
Expected stdout:
[('EU', '7')]
Reading the result
Do not sort or concatenate components unless the contract calls for it. Sorting changes key meaning, and concatenation can create ambiguous values such as AB/C and A/BC.
Validate that both selected key headers exist before reading data. Otherwise a missing field can turn every tuple into an empty or default-like value and create a misleading duplicate flood.
The order of components appears in the output tuple and should appear in the contract too. A reader must be able to reproduce the same key without guessing field precedence.
Sources
- Python csv module documentation
Prepared with AI assistance. The example uses synthetic data; its stated limits apply.