Batch transforms in pure Python
A batch is a fixed set of rows that you process in one run. A transform turns that set into a new set. Pure Python means no pandas (a popular table library): only dicts, lists and the standard library.
The senior Python round asks for this. You write a clean transform, then you scale it and explain the good and bad sides of each choice. This chapter gives you the patterns for both steps.
Here is the shop again, as Python data. Money is Decimal, as you learned in Types, Time, Nulls & Numeric Correctness. Most examples in this chapter start from these three lists:
| order_id | customer_id | status | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | paid | 40.00 | 2026-01-03 |
| 102 | 1 | paid | 25.00 | 2026-01-10 |
| 103 | 2 | refunded | 60.00 | 2026-01-11 |
| 104 | 3 | paid | 15.50 | 2026-01-15 |
| 105 | 3 | pending | None | 2026-01-20 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| customer_id | name | country |
|---|---|---|
| 1 | Asha | IN |
| 2 | Ben | GB |
| 3 | Carla | US |
| 4 | Dev | None |
| 5 | Omar | IN |
| order_id | product | qty |
|---|---|---|
| 101 | Pen | 2 |
| 101 | Notebook | 1 |
| 102 | Bag | 1 |
| 103 | Lamp | 1 |
| 104 | Pen | 3 |
| 106 | Chair | 1 |
| 106 | Lamp | 1 |
from decimal import Decimal
orders = [
{"order_id": 101, "customer_id": 1, "status": "paid",
"amount": Decimal("40.00"), "order_date": "2026-01-03"},
{"order_id": 102, "customer_id": 1, "status": "paid",
"amount": Decimal("25.00"), "order_date": "2026-01-10"},
{"order_id": 103, "customer_id": 2, "status": "refunded",
"amount": Decimal("60.00"), "order_date": "2026-01-11"},
{"order_id": 104, "customer_id": 3, "status": "paid",
"amount": Decimal("15.50"), "order_date": "2026-01-15"},
{"order_id": 105, "customer_id": 3, "status": "pending",
"amount": None, "order_date": "2026-01-20"},
{"order_id": 106, "customer_id": 4, "status": "paid",
"amount": Decimal("80.00"), "order_date": "2026-01-21"},
]
customers = [
{"customer_id": 1, "name": "Asha", "country": "IN"},
{"customer_id": 2, "name": "Ben", "country": "GB"},
{"customer_id": 3, "name": "Carla", "country": "US"},
{"customer_id": 4, "name": "Dev", "country": None},
{"customer_id": 5, "name": "Omar", "country": "IN"},
]
order_items = [
{"order_id": 101, "product": "Pen", "qty": 2},
{"order_id": 101, "product": "Notebook", "qty": 1},
{"order_id": 102, "product": "Bag", "qty": 1},
{"order_id": 103, "product": "Lamp", "qty": 1},
{"order_id": 104, "product": "Pen", "qty": 3},
{"order_id": 106, "product": "Chair", "qty": 1},
{"order_id": 106, "product": "Lamp", "qty": 1},
]
print(len(orders), len(customers), len(order_items))
print(orders[0]["order_id"], orders[0]["amount"])| output |
|---|
| 6 5 7 |
| 101 40.00 |
The next sections teach one pattern each, with code and its real output. The last sections cover tests, checks, cost and scale.
