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.
print(). In a .py file and in the editor here, wrap it in print(), as the run below does.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)[1, 4, 9, 16, 25, 36, 49] [4, 16, 36] [1, 9, 25, 49]
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:
[expression for item in items]
[expression for item in items if condition]Collecting one field with a loop
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?"},
]texts = []
for ticket in tickets:
texts.append(ticket["text"])
print(texts)['I was charged twice for one order', 'My parcel has not arrived', 'Can I get a refund for the blue mug?']
That took four lines to collect one field. A list comprehension builds the same list:
Collecting it with a comprehension
texts = [ticket["text"] for ticket in tickets]
print(texts)['I was charged twice for one order', 'My parcel has not arrived', 'Can I get a refund for the blue mug?']
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
ids = [ticket["id"] for ticket in tickets if "refund" in ticket["text"].lower()]
print(ids)[3]
An if at the end keeps only the items that pass, like the filter in Lists of dictionaries.
Building a dictionary the same way
customers = {ticket["id"]: ticket["customer"] for ticket in tickets}
print(customers)
print(customers[2]){1: 'Asha', 2: 'Ben', 3: 'Chen'}
BenCurly 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
| Comprehension | for loop | |
|---|---|---|
| Builds a list | Yes, as one expression | Yes, with append |
| Prints, counts or does several things | No | Yes |
| Best for | Collecting or filtering one field | Anything 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.
Related
- Previous: Lists of dictionaries
- Next: Functions
- Reference: List Comprehensions in the Python tutorial
- 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
ifto keep tickets whose id is greater than 1.
Little by little, you're building something great.