Skip to content
IT Vizag
← Lists, Dicts & Data

▶ 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

Lesson 6 of 9 · 1 min read

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.

🔒 Code example: sign up free (above) to unlock it.

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

🔒 Code example: sign up free (above) to unlock it.

Inside an f-string with double quotes, use single quotes for the keys.

Filtering rows

🔒 Code example: sign up free (above) to unlock it.

Sorting rows

🔒 Code example: sign up free (above) to unlock it.

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

🔒 Code example: sign up free (above) to unlock it.

setdefault(key, []) returns the list for that key, creating an empty one the first time.

Tip: Filtering, sorting and grouping are what pandas does for big tables. Learning them by hand first makes pandas much easier later.

In one line

A list of dictionaries is a table in Python: loop to read, filter, sort with key= and group with setdefault.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend