Guide Python Intermediate

12.15 File Handling

Reading unique values directly into a set with a comprehension over file lines, sorting before writing since sets have no guaranteed order, and using a set comprehension to deduplicate while reading a CSV.

2 min read

Every line read from a file arrives as str (see 8.12 Type Conversion in File Handling) — a set comprehension is the standard one-line pattern for turning those lines directly into unique values.

Reading Data into Sets

>>> with open("ips.txt") as f:
...     unique_ips = {line.strip() for line in f}
...
>>> unique_ips
{'10.0.0.1', '10.0.0.2'}

Writing Sets

Since sets have no guaranteed order (see 12.6 Set Indexing and Ordering), sort before writing if the output order matters for the reader.

with open("unique_ips.txt", "w") as f:
    for ip in sorted(unique_ips):
        f.write(ip + "\n")

CSV Deduplication

Using a set comprehension while reading a CSV to drop duplicate rows/values in a single pass.

>>> import csv
>>> with open("data.csv") as f:
...     unique_values = {row[0] for row in csv.reader(f)}
...
>>> unique_values
{'a', 'b'}

Quick Interview Answer

“Reading unique values from a file is a one-line set comprehension over the lines — {line.strip() for line in f} — which both strips the trailing newline and deduplicates in the same pass. The one thing to remember when writing a set back out is that it has no order to preserve, so the output should be sorted explicitly (sorted(unique_ips)) rather than trusting iteration order to be stable or meaningful for whoever reads the file next. The same comprehension pattern works directly against csv.reader()’s rows for one-pass CSV deduplication.”

Common Mistakes

  • Writing a set’s contents to a file by iterating it directly without sorting — the row order in the output file can vary between runs.
  • Forgetting every value read from a text file or CSV is a str, even once collected into a set — numeric comparisons or arithmetic on the set’s contents need explicit conversion first.
  • Re-reading the same file into a fresh set every time inside a loop instead of building the set once outside the loop — repeated, unnecessary I/O.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form