Records as Dictionaries

Name the columns, and the code stops depending on their order.

A row that says what it holds

Once each row is a dictionary, position stops mattering. row["score"] survives someone inserting a column; row[1] does not.

rows = [
    {"name": "Ada", "score": 92},
    {"name": "Grace", "score": 88},
]

for row in rows:
    print(row["name"], row["score"])

Missing keys, and .get

Real data has gaps. row["score"] on a row without one raises KeyError; row.get("score") hands back None instead, which is a decision you should make on purpose rather than by accident.

row = {"name": "Alan"}
print(row.get("score"))
print(row.get("score", 0))

Exercise

Try It Yourself

Each row is a dictionary. Print the name of every student whose score is at least 85, one per line.

Press Run to see output

Check Your Understanding