Built-ins that take iterables

Many built-ins accept any iterable, so they work on lists, sets, dict keys, and generators alike:

  • sum(xs), sum(xs, start).
  • min(xs) and max(xs), with key= to compare by something else. On an empty input they raise ValueError, unless you pass default=. Interviewers watch for that empty case.
  • any(xs) is true if at least one item is truthy; all(xs) if every item is. Both stop early. Note all([]) is True.
  • sorted(xs) always returns a new list. reversed(xs) returns a lazy iterator and needs a sequence, not a generator.
  • enumerate(xs, start) and zip(a, b) pair things up, also lazily. zip(..., strict=True) raises if the lengths differ.

Knowing these keeps solutions short. max(scores, key=scores.get) finds the key with the highest value in one line.

Run it, then remove default=None and see the error.