Python, properly · Chapter 35 of 42

Standard-library power tools

By the end of this chapter you can count with Counter, group with defaultdict, keep a window with deque, chain and combine iterables with itertools, and reach for functools instead of writing those loops by hand. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Pythonic and typed
  • 5 tasks
  • Python
  • In the all labs plan

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

Why you need this

Farah wants the best sellers, Bhavna wants customers grouped by area for the riders' routes, and Rafiq wants a running average of the day's totals. You can write each of these with loops and dictionaries, and you have. Each loop is ten lines with its own small bugs: a missing key, a group split in two, a window that is slow.

Python ships with tools that do these jobs in one line, written in C, tested by millions of programs: the collections, itertools and functools modules. Knowing them is the difference between code that works and code that other Python developers read at a glance, and they come up in interviews constantly.

What you build: 5 tasks

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

  1. Task 1

    top_dishes.py: the most ordered dishes

    Write dish_counts(path), a Counter of how many of each dish were ordered in the orders CSV at path, adding up the quantities (roti:2 is 2 rotis), and top_dishes(path, n), the n most ordered as a list of (dish id, quantity) pairs, most first.

    Leave out cancelled orders (the status is in mixed case on some rows), and count order D0101 once: it appears twice in the file. The check runs both on /workspace/data/orders.csv.

  2. Task 2

    by_area.py: customers by area

    Write customers_by_area(path): a dict from each area in the customers CSV at path to the list of customer names in that area, in the order of the file. Customers with a blank area (C013 and C047) go under unknown. The check compares every group on /workspace/data/customers.csv.

  3. Task 3

    per_day.py: orders per day

    Write per_day(rows): rows is any iterable of order dicts, each with a placed_at like 2026-09-01 13:41. Return a list of (day, count) tuples, one per day, in date order, the day as text like 2026-09-01.

    The check gives it every order of orders.csv in file order, then shuffled, and expects the same 30 days and counts both times.

  4. Task 4 · interview question

    Interview: moving_average.py, a moving average

    Write moving_average(values, window), a generator: for each run of window values in a row, it yields their average. Nothing is yielded until the first window is full. moving_average of 100, 200, 300, 400 with a window of 2 gives 150.0, 250.0, 350.0; a window bigger than the input gives nothing.

    values can be any iterable, including a generator. The check takes two averages from a source that fails if read too far, and runs 200,000 values with a window of 1000, which must not be slow.

  5. Task 5

    combos.py: combo deals

    Write combo_deals(path, budget): from the menu JSON at path, every pair of two different available dishes whose prices add up to at most budget, as (first name, second name, total) tuples. In each pair the dishes are in menu order; the list is sorted by total, then by the first name, then the second. The check runs it with budgets of 100, 200, 250 and 10.

What you will learn

  • Counter: counting, top N, counts that add up.
  • defaultdict: groups with no "is the key there yet?" check.
  • deque: fast at both ends, and a window with maxlen.
  • itertools: islice, chain, accumulate, pairwise, combinations, batched, and the groupby trap.
  • functools: partial and reduce (cache was chapter 31).

More in Pythonic and typed

Build "Standard-library power tools" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. 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, properly are free. This one comes with the all labs plan.