Stop 04 · FunctionsTopics · 7/8

Built-in Functions (map, filter, sorted, any, all)

Part of the Python Roadmap

In short

Python ships with a toolbox of functions you can use without importing anything. Knowing map, filter, sorted, any and all, plus sum, min, max and len, turns five-line loops into one clear line.

Python ships with handy built-ins: map() transforms every item, filter() keeps matching items, sorted() returns a new sorted list, and any() and all() check conditions across a collection. Others like sum, min, max and len are used daily.

The jaadu works beside a conveyor belt of mangoes, polishing some and picking bruised ones into a basket as the belt carries the rest into neat rows.

Think of it like this

A mango sorting line

At a mango packing shed, a conveyor belt carries fruit past workers. One worker polishes every mango (map). One removes the bruised ones (filter). One arranges the rest by size (sorted). The supervisor asks two questions: is any mango rotten? (any) Are all of them labelled? (all)

Watch it happen

prices = [499, 1299, 89, 2599, 349]

589
0
1533
1
105
2
3067
3
412
4

1/5map(lambda p: round(p * 1.18), prices) transforms every item: 589, 1533, 105, 3067, 412.

The big ideas

What you need to know

map transforms

map(func, items) applies func to every item. It returns a lazy iterator, so wrap it in list() to see the results.

filter keeps matches

filter(func, items) keeps items where func returns something truthy. Also lazy, so use list() to see them.

sorted returns a new list

sorted(items, key=..., reverse=True) works on any iterable and never changes the original.

any and all

any() is True if at least one item is truthy, all() if every item is. Both short-circuit, and all([]) is True.

See it in code

Walk through the code

python
1prices = [499, 1299, 89, 2599, 349]2 3with_gst = list(map(lambda p: round(p * 1.18), prices))4print(with_gst)                      # [589, 1533, 105, 3067, 412]5 6under_500 = list(filter(lambda p: p < 500, prices))7print(under_500)                     # [499, 89, 349]8 9print(sorted(prices))                # [89, 349, 499, 1299, 2599]10print(sorted(prices, reverse=True))  # biggest first11print(prices)                        # unchanged: sorted() returns a new list12 13print(any(p > 2000 for p in prices))  # True, at least one14print(all(p > 50 for p in prices))    # True, every one15print(sum(prices), min(prices), max(prices), len(prices))  # 4835 89 2599 5

1/5map applies the GST calculation to each price. list() collects the results.

One price list, many questions answered with built-ins and no manual loops.

Save yourself the pain

Common mistakes

Oops

print(map(str.upper, names)) and seeing <map object at 0x...>.

Do this instead

map and filter are lazy. Wrap them: list(map(str.upper, names)).

Oops

Writing prices = prices.sort() and ending up with None.

Do this instead

list.sort() sorts in place and returns None. Use sorted(prices) for a new list, or just prices.sort() on its own line.

Oops

Using map and filter with long lambdas that are hard to read.

Do this instead

For anything non-trivial, a comprehension ([p * 2 for p in prices if p < 500]) or a named function is often clearer.

Your mission

Analyse a week of spending

~25 min

Quick check

What does all([]) return?

Go deeper

  1. 1Use sorted() with key= and reverse=True to rank products by price.
  2. 2Check if any marks are below 35 with any() and a generator expression.
  3. 3Compare map() and filter() with the equivalent list comprehension.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge