1.2.33Introduction to Programming (Python)

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

2,100 words10 min readdifficulty · medium1 backlinks

The mental model: each function answers ONE question

Function Question it answers Returns
map(f, xs) "Apply f to each item" lazy iterator
filter(f, xs) "Keep items where f(item) is truthy" lazy iterator
zip(a, b) "Pair up items position-by-position" lazy iterator of tuples
enumerate(xs) "Give me index and item" lazy iterator of (i, item)
sorted(xs) "Give a new ordered list" list
reversed(xs) "Walk it back-to-front" lazy iterator
min / max "Smallest / largest" one element
sum(xs) "Add them all" number
any(xs) "Is at least one truthy?" bool
all(xs) "Are all truthy?" bool

1. Transforming — map

map can take multiple iterablesf then receives one item from each:

list(map(lambda a, b: a + b, [1,2,3], [10,20,30]))  # [11, 22, 33]

Why this step? It stops at the shortest iterable, just like zip.


2. Selecting — filter


3. Pairing — zip and 4. Indexing — enumerate

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

5. Ordering — sorted (and 6. reversed)


7–11. Summarizing — min, max, sum, any, all


Recall Feynman: explain to a 12-year-old

Imagine a line of kids each holding a card.

  • map: every kid changes their card by the same rule (double the number).
  • filter: only kids with a red card stay; the rest sit down.
  • zip: line up the boys row and the girls row and hold hands in pairs.
  • enumerate: a teacher walks the line saying "you’re #1, you’re #2…".
  • sorted: rearrange kids tallest-to-shortest into a new line (old line untouched).
  • reversed: walk the same line from the back.
  • min/max/sum: find the shortest kid / tallest kid / total height.
  • any: "is anyone wearing red?" — all: "is everyone wearing red?"

Common mistakes (Steel-man + Fix)


Flashcards

What does map(f, xs) return and is it lazy?
An iterator yielding f(x) for each x; yes, lazy and single-use — wrap in list() to see it.
How is enumerate related to zip?
enumerate(xs)zip(itertools.count(start), xs); it pairs an index with each item.
Why does all([]) return True?
Vacuous truth — all is AND over items; AND's identity element is True, so an empty all is True.
Why does any([]) return False?
any is OR over items; OR's identity is False, so empty any is False.
Difference between sorted(xs) and xs.sort()?
sorted returns a NEW list (no mutation); .sort() mutates in place and returns None.
What does the key argument do in sorted/min/max?
Maps each item to a value to compare BY; the items themselves are kept/returned.
How do you unzip pairs = [(1,'a'),(2,'b')]?
nums, letters = zip(*pairs).
What does zip do when iterables differ in length?
Stops at the shortest one.
Why does print(filter(...)) show <filter object>?
filter is a lazy iterator; wrap in list() to materialize values.
What does filter(None, xs) do?
Keeps only the truthy items (drops 0, '', None, [], etc.).
Are any/all short-circuit?
Yes; any stops at first truthy, all stops at first falsy.
Equivalent of sum(['x','y']) for strings?
''.join(['x','y']) — sum starts at 0 and can't add to a string.

Connections

  • List comprehensions — often a clearer alternative to map/filter: [x**2 for x in xs].
  • Lambda functions — the tiny f you pass to map/filter/sorted.
  • Iterators and generators — explains "lazy" and "single-use".
  • functools.reduce — the general reducer behind sum/min/max.
  • Truthiness in Python — drives filter, any, all.
  • Sorting algorithms — what sorted (Timsort) does under the hood, and stability.

Concept Map

replaced by

transform each

keep truthy

pair by position

index plus item

reorder

summarize

returns

returns

returns

returns

stops at shortest, like

wrap in list to see

Hand-written for-loops

11 built-in functions

Lazy iterators

map f xs

filter pred xs

zip a b

enumerate xs

sorted / reversed

min max sum any all

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, in built-in functions ka basic idea simple hai: jo kaam tum baar-baar for loop likh ke karte ho, unke liye Python ne ready-made naam de diye hain. map matlab har item pe ek rule apply karo (jaise har number ko square). filter matlab sirf woh items rakho jinpe condition True ho (jaise sirf even numbers). zip do lists ko position-wise jod deta hai — pehle ke saath pehla, doosre ke saath doosra. enumerate tumhe loop ke andar index bhi deta hai aur value bhi, taaki range(len(...)) ka jhanjhat na ho.

Phir aate hain ordering wale: sorted ek nayi sorted list deta hai (original ko chhedta nahi), aur key argument batata hai ki kis cheez ke hisaab se sort karna hai (jaise key=len se length ke hisaab se). reversed ulta ghuma deta hai. Aur min, max, sum, any, all poori list ko ek answer me daba dete hain — sabse chhota, sabse bada, total, "koi ek bhi True?", aur "sab ke sab True?".

Ek important baat yaad rakhna: map, filter, zip, enumerate, reversed sab lazy hote hain. Matlab print karoge toh <map object> jaisa kuch dikhega — value dekhne ke liye list(...) me lapet do. Aur ye ek hi baar consume hote hain; doosri baar loop karoge toh khaali aayega, isliye zaroorat ho toh list() me ek baar store kar lo.

Yaad rakhne ka funda: all([]) True deta hai (vacuous truth — khaali AND hamesha True) aur any([]) False deta hai. Exam aur real coding dono me ye 11 functions tumhare 80% loops ko chhota aur clean bana denge — isliye inhe ratne se zyada kab kaunsa lagana hai, woh samajhna zaroori hai.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections