Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

List comprehensions

A list comprehension is an expression that builds a new list from another: what to put in, a for clause, and an optional if, inside square brackets.

Last updated: 30 Sep, 2026 · Python 3.14

Lists of dictionaries built a list with an empty list, a loop and append. That pattern is so common that Python has a one-line form for it.

From a loop with append to a comprehension · from Tutorial 17- Python List Comprehension · 2:26 to 7:06
The video starts from a loop inside a function that appends to a list made outside it; the page's loop version below builds its list in place. The main gain of a comprehension is a shorter line, and any speed difference is small. The video runs each line in a Jupyter notebook, which shows the value of a cell's last line without print(). In a .py file and in the editor here, wrap it in print(), as the run below does.
ExampleFrom the video, run here
lst = [1, 2, 3, 4, 5, 6, 7]
lst1 = [i*i for i in lst]
print(lst1)

lst1 = [i*i for i in lst if i % 2 == 0]
print(lst1)
lst1 = [i*i for i in lst if i % 2 != 0]
print(lst1)

The expression i*i comes first, then the for, then the if that keeps only even or only odd numbers. Below, the same form collects fields from the tickets.

Syntax:

python
[expression for item in items]
[expression for item in items if condition]

Collecting one field with a loop

Examplethe tickets from the Lists of dictionaries lesson
tickets = [
    {"id": 1, "customer": "Asha", "text": "I was charged twice for one order"},
    {"id": 2, "customer": "Ben", "text": "My parcel has not arrived"},
    {"id": 3, "customer": "Chen", "text": "Can I get a refund for the blue mug?"},
]
Example
texts = []
for ticket in tickets:
    texts.append(ticket["text"])
print(texts)

That took four lines to collect one field. A list comprehension builds the same list:

Collecting it with a comprehension

Example
texts = [ticket["text"] for ticket in tickets]
print(texts)

Read the brackets as: a list of ticket["text"], for each ticket in tickets. The part before for is what goes into the new list.

Keeping some items with if

Example
ids = [ticket["id"] for ticket in tickets if "refund" in ticket["text"].lower()]
print(ids)

An if at the end keeps only the items that pass, like the filter in Lists of dictionaries.

Building a dictionary the same way

Example
customers = {ticket["id"]: ticket["customer"] for ticket in tickets}
print(customers)
print(customers[2])

Curly brackets and a key: value pair make a dictionary instead. Looking a customer up by id is now one step instead of a loop.

Comprehension vs for loop

Comprehensionfor loop
Builds a listYes, as one expressionYes, with append
Prints, counts or does several thingsNoYes
Best forCollecting or filtering one fieldAnything with more steps

Where comprehensions show up in AI code

  • Pulling the text out of every record before sending a batch to a model.
  • Keeping only the answers that passed a check.
  • Building the list of calls that asyncio.gather starts together.
Watch out. A comprehension that needs a second line of thought is harder to read than a loop. When it prints, counts or does more than one thing per item, write the loop.
Try it yourself
  • Make a list of the customers' names in capital letters.
  • Make a list of the lengths of each ticket's text.
  • In the ids example, change the if to keep tickets whose id is greater than 1.

Little by little, you're building something great.