Split an order across warehouses
Fulfilment needs the order regrouped by the warehouse each line ships from, so it can raise one pick list per site.
- Group the SKUs under their warehouse code.
- Within a warehouse, keep the order the lines arrived in.
- A warehouse with no lines does not appear at all.
split_by_warehouse(rows: list<Row>) → map<string, list<string>>
Where you start
def split_by_warehouse(rows: list[Row]) -> dict[str, list[str]]:
Worked examples
| Call | Result |
|---|---|
split_by_warehouse([Row(sku="A1", warehouse="IST"), Row(sku="B2", warehouse="ANK"), Row(sku="C3", warehouse="IST")]) | {"IST": ["A1", "C3"], "ANK": ["B2"]} |
split_by_warehouse([Row(sku="A1", warehouse="IST")]) | {"IST": ["A1"]} |
split_by_warehouse([]) | {} |
split_by_warehouse([Row(sku="X", warehouse="IZM"), Row(sku="X", warehouse="IZM")]) | {"IZM": ["X", "X"]} |
Hint
A map from code to a growing list. Create the list the first time you meet a code.
Reference solution in Python
def split_by_warehouse(rows: list[Row]) -> dict[str, list[str]]:
result = {}
for r in rows:
result.setdefault(r.warehouse, []).append(r.sku)
return result