Module 12: Iteration and the standard toolkit

Comprehensions, iterators and generators, and the built-ins and modules (collections, heapq, itertools) that save you code.

  1. Comprehensions in depth: [expr for x in it if cond]: filter if at the end, a if c else b at the front; {k: v ...} makes a dict, {v ...} a set; nested fors read like nested loops.
  2. Iterators and generators: for uses iter() and next() until StopIteration; a generator (yield or (x for x in …)) makes values lazily, saves memory and can be used only once.
  3. Built-ins that save you: enumerate for positions, zip for parallel lists (stops at the shortest), any/all for conditions, key= for sorting (stable, use a tuple for ties), divmod for quotient and remainder.
  4. The collections module: Counter counts (mostcommon(k)), defaultdict groups and totals, deque gives O(1) ends (with maxlen), namedtuple names tuple fields, OrderedDict adds movetoend.
  5. heapq and bisect: heapq is a min-heap on a list (h[0] smallest, push/pop O(log n)); negate for a max-heap, use nlargest(k, …) for top k; bisect searches and insort inserts into a sorted list in O(log n).
  6. itertools and functools: permutations (order matters), combinations (it doesn't), product (nested loops), accumulate (running totals), groupby (consecutive only, sort first), islice/chain for iterators; partial pre-fills arguments, cmptokey sorts with a compare function.

Ends with a 14-question checkpoint (80% passes) and a mini project: Canteen Counter: the sales log analyser.