Skip to content

Lambda and Built-in Functions

Use short lambda functions and Python built-ins to sort, transform, filter, and check data.

12 min read 10 quiz questions

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
Example 1: Sort with a lambda key
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

Example 2: Transform and filter values
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

FunctionWhat it doesExample
sumAdds valuessum([2, 3]) -> 5
min / maxFinds the smallest / largest valuemax([2, 7]) -> 7
absGets distance from zeroabs(-4) -> 4
roundRounds a numberround(3.14159, 2) -> 3.14
any / allChecks whether any / all values are trueall([True, False]) -> False
sortedReturns a new sorted listsorted([3, 1]) -> [1, 3]
reversedGives items in reverse orderlist(reversed([1, 2])) -> [2, 1]
zipPairs items from iterableslist(zip([1, 2], ['a', 'b'])) -> [(1, 'a'), (2, 'b')]
lenCounts itemslen('hi') -> 2
Example 3: Combine built-ins
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.

Example
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

  1. sorted() orders pairs using whatever key() returns.
  2. lambda pair: pair[1] says 'compare by the marks slot'.
  3. reverse=True puts the highest first.

References & Further Reading

Related entries from PyLabs's own reference library.

  • sorted() (Make a new list containing items in sorted order.)
  • sort() (Sort the elements of the list in ascending or custom order.)
  • filter() (Keep items that pass a test.)

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Practice exercise

Not passed yet

Use 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
Want another challenge on this module? More coding practice

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 completed

Create an ID or sign in to save lesson completion across devices.

Create ID to save
Module lesson progress0/5 complete
Module cheat sheet