Nested data — a day’s timetable

A timetable is a list of periods, and each period is a record (subject + room). Nested lists and dictionaries model real school data. This is the same shape as a JSON API and as a pandas DataFrame made of rows.

Period timetable

Nested records model a school day.

You will be able to

  • Store a list of dictionaries
  • Loop rows and pick columns by key

Rows in a table

Each dict is a row. The list is the table. Loop rows, then pick columns by key. You never write period[0] for the subject if the key is "subject" — names survive reordering of fields.

When a period gains a teacher field, old loops that only print subject still work. That is why structured records beat a list of anonymous triples.

This shape is everywhere later

A CSV file is this table stored as text. A SQLite query returns this table. pandas calls it a DataFrame. JSON APIs return a list of objects. Intermediate Python is not a different language from Advanced — Advanced just puts this table on disk and gives it libraries.

When you print period["subject"], "in", period["room"], you are already writing a report. The next step is storing day in a file so Monday still exists after you close the program.

Keep keys consistent

"room" and "Room" are different keys. Pick a style (lowercase, short English words) and never mix. A later merge of two files fails for months because one lab wrote Class and another wrote class.

If a period is cancelled, do not delete mystery indexes. Prefer a field like "status": "cancelled" so the rest of the row still exists for the audit log.

Words that matter

Nested data
Collections inside collections — a list of dicts.

Common mistakes

Avoid: period["room"] when you typed "Room" with a capital R.

Do this: Keys are exact strings. Stay consistent.

Example program — Monday morning

Add a period and Run.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Your two periods

Include a period whose subject is English. Print subjects so English appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. period["room"] fails if…

Progress is stored in a browser cookie on this device.