compare approaches one problem · two ways
Compare Approaches
The same problem solved different ways in Python, run on the same input, with what really differs and when to use each.
csv.reader (indexes) vs csv.DictReader (names)
Print the first few planets with their discovery year and method.
what differs
reader yields lists, so you look positions up yourself; DictReader yields dicts keyed by the header, slightly slower but self-describing.
at a glance
| approach | use it when |
|---|---|
| A · csv.reader | Very wide files where speed matters, or files without a header. |
| B · csv.DictReader | Almost always: code reads by column name and survives column reordering. |
Watch out: Duplicate header names collapse in DictReader (the last wins). Missing trailing fields become None.
the outputs
Each approach runs on the same input.
concepts
csv.DictReader
learn it
related comparisons