Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
A partner re-uploads yesterday's order file whenever their job retries, so the landing zone regularly contains the same rows twice. The loader downstream is not idempotent, and duplicated rows become duplicated revenue.
Write dedupe(rows, key_fields). It takes the rows and the list of fields that together identify a row, and returns the rows with the repeats removed.
Function to write
dedupe(rows: list[dict], key_fields: list[str]) -> list[dict]The rows with repeated keys removed, in the order they arrived.
How to approach it
A set for membership and a list for the answer — the set is never what you return.
Sample cases
+ 2 held back until you submit
a replayed order file
Two rows are exact replays and one repeats the key with a different quantity; the first of each wins.
Input
Argument 1
| order_id | line | sku | qty |
|---|---|---|---|
| o1 | 1 | A1 | 2 |
| o1 | 2 | B2 | 1 |
| o2 | 1 | A1 | 5 |
| o1 | 1 | A1 | 9 |
| o3 | 1 | C3 | 1 |
| o2 | 1 | A1 | 5 |
Argument 2
[
'order_id',
'line'
]Returns
| order_id | line | sku | qty |
|---|---|---|---|
| o1 | 1 | A1 | 2 |
| o1 | 2 | B2 | 1 |
| o2 | 1 | A1 | 5 |
| o3 | 1 | C3 | 1 |
a single-field key
The same rows keyed only by order_id collapse to one row per order.
Input
Argument 1
| order_id | line | sku | qty |
|---|---|---|---|
| o1 | 1 | A1 | 2 |
| o1 | 2 | B2 | 1 |
| o2 | 1 | A1 | 5 |
| o1 | 1 | A1 | 9 |
| o3 | 1 | C3 | 1 |
| o2 | 1 | A1 | 5 |
Argument 2
[
'order_id'
]Returns
| order_id | line | sku | qty |
|---|---|---|---|
| o1 | 1 | A1 | 2 |
| o2 | 1 | A1 | 5 |
| o3 | 1 | C3 | 1 |
no rows
An empty file produces an empty list without touching the first element.
Input
Argument 1
[] (empty list)
Argument 2
[
'order_id'
]Returns
[] (empty list)
Constraints
key_fields are equal, in the order the fields were given.None, so a missing field and an explicit null are the same value.key_fields means there is nothing to compare, so every row survives.Worked example
Keyed on order_id and line, the file has six rows and four keys. Row 4 repeats the key of row 1 but carries a quantity of 9 instead of 2 — it is dropped, because the rule is that the first row wins, and the difference is not a signal to keep both.
The instinct is to reach for set(rows), and it does not work: a dict is not hashable, so Python raises before the first comparison. The instinct after that is list(set(...)) over some tuple form, which does run — and returns the rows in whatever order the hash table produced, which is a different order on a different input. Order is the part that is easy to lose without noticing.
What this tests
Building a composite key, and the difference between a set used for membership and a set used to hold the result. One preserves order because you never iterate it; the other loses it.
dedupe(rows: list[dict], key_fields: list[str]) -> list[dict]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.