runtime

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

approachuse it when
A · csv.readerVery wide files where speed matters, or files without a header.
B · csv.DictReaderAlmost 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

Read CSV rows as dictionaries

related comparisons

csv module vs pandas

Uses the Exoplanets dataset.

A · csv.reader
B · csv.DictReader
Compare Python Approaches · Runtime