Python traps · Chapter 6 of 16

Modifying a list or dict while iterating

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: a cancelled order left in the batch

    Write reproduce() in repro.py. Make a batch: a list of four orders in this order, each a dict with "id", "rider" and "status": D2301 "packed", D2302 "cancelled", D2303 "cancelled", D2304 "packed" (any riders you like). Give the list to drop_cancelled from batch.py, then return the ids of the orders left in your list, in order.

    The check runs your reproduce() twice: beside the original batch.py it must return ['D2301', 'D2303', 'D2304'] (D2303, cancelled, missed), and beside a fixed batch.py it must return ['D2301', 'D2304']. So it must really call drop_cancelled and then look at the list it passed in. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    batch.py: every cancelled order leaves the batch

    Fix batch.py. drop_cancelled(orders) removes every order whose status is "cancelled" from the list it was given, the same list object (the riders' app holds it), keeps the other orders in their order, and returns how many it removed. drop_cancelled_ids(by_id) does the same for a dict of id to order: removes the cancelled ones from that same dict, never raises, and returns how many. Both work with cancelled orders side by side, three in a row, first and last, all, none, and an empty batch.

    Keep the log line, clean-up: dropped <id>, printed once for each order removed. The check imports a fresh copy of your batch.py and uses its own orders, and it looks at the list and the dict it passed in: a fix that builds a new list and only gives it the parameter's name leaves the dispatcher's list as it was.

  3. Task 3

    test_batch.py: a test that would have caught it

    Write a pytest file, test_batch.py, that would have caught this bug before a rider rode out with a cancelled tiffin. The check runs your file three times: beside the original batch.py, where at least one test must fail; beside the fixed batch.py, where every test must pass; and beside a wrong fix that builds a new list and leaves the caller's list as it was, where at least one test must fail.

    So put two cancelled orders side by side, call drop_cancelled, and check the list you passed in: neither cancelled order is left in it and the others are, in order. Checking only the number returned is not enough. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: a cancelled order left in the batch
  • batch.py: every cancelled order leaves the batch
  • test_batch.py: a test that would have caught it

More in Everyday traps

Build "Modifying a list or dict while iterating" yourself

DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps are free. This one comes with the all labs plan.