Lambda and Built-in Functions
Use short lambda functions and Python built-ins to sort, transform, filter, and check data.
Small one-line functions
A lambda is a small anonymous function written on one line. It takes inputs before the colon and has one expression after it.
Lambdas are useful when another function needs a quick rule.
lambda parameters: expression
words = ["pear", "fig", "banana"] by_length = sorted(words, key=lambda word: len(word)) print(by_length)
sorted calls the lambda for each word. The returned length becomes the sort key, so shorter words appear first.
Map and filter
numbers = [1, 2, 3, 4, 5] squares = list(map(lambda number: number * number, numbers)) evens = list(filter(lambda number: number % 2 == 0, numbers)) print(squares) print(evens)
map applies a function to every item. filter keeps only items for which its function returns True. Converting them to a list lets you see the results.
Useful built-in functions
| Function | What it does | Example |
|---|---|---|
| sum | Adds values | sum([2, 3]) -> 5 |
| min / max | Finds the smallest / largest value | max([2, 7]) -> 7 |
| abs | Gets distance from zero | abs(-4) -> 4 |
| round | Rounds a number | round(3.14159, 2) -> 3.14 |
| any / all | Checks whether any / all values are true | all([True, False]) -> False |
| sorted | Returns a new sorted list | sorted([3, 1]) -> [1, 3] |
| reversed | Gives items in reverse order | list(reversed([1, 2])) -> [2, 1] |
| zip | Pairs items from iterables | list(zip([1, 2], ['a', 'b'])) -> [(1, 'a'), (2, 'b')] |
| len | Counts items | len('hi') -> 2 |
values = [-3, 2, 5] print(sum(values)) print(min(values), max(values)) print(abs(-8), round(3.14159, 2)) print(any([False, True]), all([True, True])) print(list(reversed(sorted(values)))) print(list(zip([1, 2], ["a", "b"])))
Key points
- A lambda has parameters, a colon, and one expression.
- Use sorted(key=...) to sort by a custom rule.
- map transforms every item and filter keeps matching items.
- Built-ins solve common tasks with short, readable code.
- sorted returns a new list rather than changing the original.
Worked example
Sort students by marks without writing a def — a lambda supplies the sort key inline.
students = [("Mim", 78), ("Rafi", 64), ("Joe", 91)]
top_first = sorted(students, key=lambda pair: pair[1], reverse=True)
print(top_first[0])
How it works, step by step
- sorted() orders pairs using whatever key() returns.
- lambda pair: pair[1] says 'compare by the marks slot'.
- reverse=True puts the highest first.
References & Further Reading
Related entries from PyLabs's own reference library.
Practice exercise
Not passed yetUse filter with a lambda to keep numbers greater than 3 from [1, 4, 2, 5]. Convert the result to a list and print it.
Expected output: [4, 5]
# Write your code below
Check yourself
Lambda and Built-in Functions quiz
10 questions. You get instant feedback per question, and the timer starts when you press the button.
Lesson incomplete
Not completedCreate an ID or sign in to save lesson completion across devices.