Guide Python Beginner

10.8 Built-in Functions

Global functions that accept a list as an argument -- len(), max(), min(), sum(), sorted(), any(), and all() -- as distinct from methods called on the list itself.

2 min read

These are called as len(l), not l.len() — global functions, not methods, the same distinction covered for strings in 9.8 String Functions.

FunctionExampleResult
len()len([3, 1, 4, 1, 5])5
max()max([3, 1, 4, 1, 5])5
min()min([3, 1, 4, 1, 5])1
sum()sum([3, 1, 4, 1, 5])14
any()any([0, 0, 1])True
all()all([1, 0, 1])False

sorted()

Returns a new sorted list, leaving the original untouched — unlike l.sort(), which sorts in place and returns None (see 10.7 List Methods).

>>> l = [3, 1, 4, 1, 5]
>>> sorted(l)
[1, 1, 3, 4, 5]
>>> sorted(l, reverse=True)
[5, 4, 3, 1, 1]
>>> l          # original is untouched
[3, 1, 4, 1, 5]

any() / all()

any() is True if at least one element is truthy; all() is True only if every element is — both short-circuit, stopping at the first element that decides the answer.

>>> any([0, 0, 1])
True
>>> all([1, 1, 1])
True
>>> all([1, 0, 1])
False

Quick Interview Answer

“These are built-in functions, not list methods — len(l), not l.len(). sum(), max(), min() do the obvious numeric thing. The one worth calling out specifically is sorted() vs. l.sort(): sorted() always returns a brand-new list and leaves the original alone, while sort() mutates in place and returns None — mixing the two up is one of the most common list-related bugs. any()/all() both short-circuit on the first element that decides the result, so they’re O(1) in the best case even on a huge list.”

Common Mistakes

  • Assuming sorted(l) mutates l — it doesn’t; the original list is completely untouched, only the returned value is sorted.
  • Calling sum() on a list containing non-numeric elements (like strings) — it raises TypeError; use "".join(...) for concatenating strings instead.
  • Forgetting any([]) is False and all([]) is True — both edge cases follow directly from their definitions (no element is truthy → any fails; no element violates truthiness → all vacuously holds) but are easy to get backwards from memory.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form