Dedupe preserving order in Python
Order-preserving dedupe - a daily-driver data cleanup.
def dedupe(items):
seen = set()
out = []
for item in items:
if item not in seen:
seen.add(item)
out.append(item)
return out
How it works
- Tracks already-seen items in a
set. - First occurrences are kept, in their original order.
- Later duplicates are skipped.
Keywords and builtins used here
dedupedefforifreturnset
The run, in numbers
- Lines
- 8
- Characters to type
- 123
- Tokens
- 40
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 27 seconds.
Step 5 of 10 in Collections, step 27 of 72 in Language basics.