▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
Lists of dictionaries
The shape of real data, like rows in a spreadsheet.
🎬 Video version coming soon — read it now
Real data usually looks like a table: many rows, each with the same columns. In Python the natural shape for that is a list of dictionaries: one dictionary per row.
This is exactly the shape you get from a CSV file, a database or a web API, which you'll meet in the Files, Modules & Packages course.
Reading rows
Inside an f-string with double quotes, use single quotes for the keys.
Filtering rows
Sorting rows
key=lambda s: s["cgpa"] tells sorted what to sort by. You'll learn lambdas properly in the advanced course; for now, read it as "sort each student by their cgpa".
Grouping rows
setdefault(key, []) returns the list for that key, creating an empty one the first time.
In one line
A list of dictionaries is a table in Python: loop to read, filter, sort with key= and group with setdefault.
Share with a friend