Lesson 1 · 1 of 20
List, dict and set comprehensions · Python
Build lists, dicts and sets in a single line with Python comprehensions, including filters and unpacking. Try it in DMarlon Studio.

A comprehension builds a new collection from another one without writing out the whole loop. [qty * price for _, _, qty, price in orders] walks through the orders, unpacks each tuple and keeps the total of every line. The underscore _ is the usual name for values you do not need.
You can add a filter at the end with if: the big list only keeps lines worth 30 or more, and the expression at the start (here the f-string f"{name}/{item}") decides what is stored for each one.
Curly braces with a single value give you a set comprehension: {item for _, item, _, _ in orders} keeps each product once. Sets have no fixed order, which is why the program passes them through sorted() before printing.
Curly braces with key: value give you a dict comprehension. spent maps every customer to what they spent, and it contains another comprehension (a generator expression inside sum()). The last one, cheap, loops over prices.items() and flips the dictionary so the price becomes the key.
Comprehensions are faster and easier to read than a loop with append() when the result fits on one line. If you need several chained conditions or side effects (printing, saving), a plain loop reads better.
Code · main.py
# Orders from an online shop: (customer, product, quantity, unit price)orders = [ ("Ana", "keyboard", 2, 25.0), ("Luis", "mouse", 1, 12.5), ("Ana", "monitor", 1, 180.0), ("Marta", "mouse", 3, 12.5), ("Luis", "cable", 4, 3.0),]# List comprehension: one total per order linetotals = [qty * price for _, _, qty, price in orders]print("Line totals:", totals)# With a condition: only the lines worth 30 or morebig = [f"{name}/{item}" for name, item, qty, price in orders if qty * price >= 30]print("Big lines:", big)# Set comprehension: each product only once (sorted to print it)products = {item for _, item, _, _ in orders}print("Products:", sorted(products))# Dict comprehension: customer -> amount spentcustomers = sorted({name for name, *_ in orders})spent = {c: sum(q * p for n, _, q, p in orders if n == c) for c in customers}print("Spent:", spent)# Turn a dict around: price -> product, for the cheap itemsprices = {"keyboard": 25.0, "mouse": 12.5, "cable": 3.0, "monitor": 180.0}cheap = {price: item for item, price in prices.items() if price < 20}print("Cheap:", cheap)Output · python3 main.py
Line totals: [50.0, 12.5, 180.0, 37.5, 12.0]
Big lines: ['Ana/keyboard', 'Ana/monitor', 'Marta/mouse']
Products: ['cable', 'keyboard', 'monitor', 'mouse']
Spent: {'Ana': 230.0, 'Luis': 24.5, 'Marta': 37.5}
Cheap: {12.5: 'mouse', 3.0: 'cable'}Key points
[expr for x in data if condition]builds a list.{expr for x in data}builds a set, with no duplicates.{key: value for x in data}builds a dict.- You can unpack inside the
for:for name, *_ in orders. - If a comprehension does not fit on one line, use a loop.
Exercise
Add a units dict that maps each product to the total units sold (for example, mouse should give 4) using a dict comprehension.