
All day we have used lists and dictionaries for very specific reasons: to create a list of column names to subset a dataframe, and to use a dictionary of old names and new ones to rename with.
This afternoon we slow down and look at both types of containers on their own, with no DataFrame in the way. A great deal of the python we write from Day 3 onwards is built out of lists and dictionaries, so it is worth one session on what they are, and not only on what they do.
How a live coding session works
We all start from an empty notebook and write everything together, live. There is no python on this page to copy, because typing a line out yourself is what makes it stick. The markdown headings below are the one exception, and you should copy those.
- Cella or Kelly writes a line, explains it, and runs it. You write the same line and run it.
- When something breaks, we fix it together. Things will break (they always do!), and working out what went wrong is usually where the useful learning happens.
- If you fall behind, say so! Wave at Cella or Kelly and we will slow down, which we would much rather do than leave anybody behind.
- Type the code yourself rather than copying it off a neighborβs screen. Typing it is how you find out which parts you have not understood yet, and it is how you meet your own typos.
Set up your notebook
Create a new notebook named
Session_2D_Lists_and_Dicts.ipynb.Add a title cell:
# Day 2: Session 2D - Lists & Dictionaries
Date: 09/01/2026- Add the following markdown cells now, in this order, leaving space under each. Weβll fill in the code as we go, and having the structure in place means your notes stay organized instead of becoming one long scroll.
## 1. Making lists
## 2. Getting things out of a list
## 3. Changing a list
## 4. Looping over a list
## 5. Making dictionaries
## 6. Getting things out of a dictionary
## 7. Looping over a dictionary
## 8. Why this mattered today- Under each heading, add an empty code cell.
The eight headings and their empty code cells are the scaffold for the whole session. Read the two short sections below before we start, and then switch to your notebook, because the coding itself happens there rather than on this page.
What weβll cover
Lists (about 10 minutes)
- writing a list, and what can go in one
- indexing from the front and from the back
- slicing out a piece
len().append()to add to the end- looping over a list with
for
Dictionaries (about 10 minutes)
- writing a dictionary, keys and values
- looking up a value by key
- adding and changing entries
.keys()and.values()- looping over a dictionary
- what happens when you ask for a key that isnβt there
Tying it back (about 5 minutes)
- the list of column names you selected with in this morningβs Session 2A
- the rename dictionary you built in this morningβs Session 2B
- one small loop that checks several columns at once
What you should be able to do afterwards
- write a list and a dictionary from scratch, without having to look up the syntax
- get an item out of either one, and say which container you would choose for a given job
- read a
forloop and predict what it prints - explain why
df[['a', 'b']]has two sets of brackets