1.2.33 · D1Introduction to Programming (Python)

Foundations — Built-in functions — map, filter, zip, enumerate, sorted, reversed, min, max, sum, any, all

1,812 words8 min readBack to topic

Before you can trust sorted(words, key=len, reverse=True), you have to know what a list is, what a lambda is, what key= means, what % computes, and why map(...) prints as <map object>. This page builds every one of those from nothing, in the order they depend on each other.


0. The most basic picture: a box holding things in a row

Everything here starts with a collection of items sitting in order. In Python the everyday version is a list, written with square brackets and commas.

Figure — Built-in functions — map, filter, zip, enumerate, sorted, reversed, min, max, sum, any, all

Why start here? Every single function on the parent page (map, filter, sorted, …) takes a box like this and does something to it. If the box and its index counting aren't crystal clear, nothing else can be.


1. Iterable — anything you can walk through one item at a time

A list is one example of a bigger idea.

Why the topic needs this word: the docs say map(f, iterable), not map(f, list), because these functions work on any walkable thing — a string, a file, a range — not just lists.


2. Function and lambda — a rule you can hand to another machine

Sometimes you need a rule so tiny it isn't worth giving a name. That's what lambda is.

Figure — Built-in functions — map, filter, zip, enumerate, sorted, reversed, min, max, sum, any, all

Why the topic needs it: map, filter, sorted, min, max all want you to pass a rule as an argument. lambda lets you write that rule inline instead of defining a whole named function first. Deep dive: Lambda functions.


3. The **, %, <, == symbols the examples use

The parent page silently uses several operators. Here they are, from zero.

Why the topic needs them: filter(lambda x: x % 2 == 0, xs) keeps even numbers precisely because x % 2 == 0 is the test "does dividing by 2 leave nothing over?"


4. Truthy / falsy — how a value counts as yes-or-no

filter, any, and all don't need a strict True/False; they accept anything and ask "does this count as yes?"

Why the topic needs it: filter(None, xs) "drops all falsy items," and any/all are asking about truthiness, not literal True. Deep dive: Truthiness in Python.


5. Laziness — the machine that waits until asked

This is the single most confusing behaviour on the parent page, so we build it carefully.

Figure — Built-in functions — map, filter, zip, enumerate, sorted, reversed, min, max, sum, any, all

Why the topic needs it: map, filter, zip, enumerate, reversed all return lazy iterators. That's why print(map(...)) shows <map object> instead of numbers — you never asked for the results. Deeper: Iterators and generators.


6. The * spread and tuple unpacking

Two last pieces of notation the parent uses in the "unzip" trick.

Why the topic needs it: the elegant zip(*pairs) "unzip" only works because * turns one list of pairs into several separate pairs handed to zip.


7. key= — sort/compare by a derived value

Why the topic needs it: without key, sorting compares whole items directly; key lets you say "compare by this aspect only." It reuses everything from §2 (functions) and §0 (indexing).


How these foundations feed the topic

List and index from 0

Iterable walk one at a time

Lazy iterator single use

Function as a rule

lambda inline rule

key argument

Operators percent power equals

Predicate true or false

Truthy and falsy

The 11 built in functions

Star spread and unpacking

Everything on the left is a prerequisite; they all flow into the eleven functions on the parent topic. From there the natural next steps are List comprehensions (a compact alternative to map+filter), functools.reduce (the general form of sum/min/max), and Sorting algorithms (what sorted does under the hood).


Equipment checklist

In [10, 20, 30], what index holds 20?
Index 1 — Python counts from 0.
What does iterable mean?
Anything you can walk through one item at a time (list, tuple, string, range).
Read lambda x: x + 1 in plain words.
"Given x, return x plus one" — a tiny nameless function.
What does 7 % 2 evaluate to and why?
1 — the remainder after dividing 7 by 2.
Difference between = and ==?
= stores a value; == tests whether two values are equal.
Name three falsy values.
0, '' (empty string), [] / None / False (any of these).
Why does print(map(str, [1,2,3])) show <map object>?
map is lazy — you never asked for the results; wrap in list(...).
What happens if you call list() twice on the same map object?
Second call gives [] — a lazy iterator is single-use / exhausted after the first walk.
What does *pairs do in zip(*pairs)?
Spreads the list into separate arguments, so each pair is handed to zip individually.
What does key=len tell sorted?
Judge/order each item by its length rather than by the item itself.