Lambda (small functions)
๐ What are Lambda Functions? A lambda is a tiny, anonymous function written in one line: lambda arguments: expression. It returns the result of the expression automatically. Lambdas are perfect when you need a quick function to pass to map(), filter(), or sorted() without writing a full def.

Appy Saysโฆ
Sometimes you need a quick, throwaway function for just one job โ like sorting a list by the second element, or filtering items on the fly. Writing a full def block feels like overkill. That's exactly when lambda saves the day.
What is a Lambda Function?
A lambda is an anonymous (unnamed) function defined in a single expression. It can take any number of arguments but has only one expression as its body โ the result of which is automatically returned.
- โขSyntax:
lambda arguments: expression - โขExample:
lambda x: x * 2โ doubles its input - โขAssign:
double = lambda x: x * 2; double(5)โ10 - โขMultiple args:
lambda x, y: x + y - โขMost useful as an argument to
sorted(),map(),filter() - โขSort by second element:
sorted(pairs, key=lambda p: p[1])
Think of it like a one-time Minecraft command block
A full def function is like a named tool in your hotbar โ you use it over and over. A lambda is like a command block you place for a single trigger: it runs, does its one job, and you move on. No need to name it.
How It Works
- โข1.
lambda x: x * 2creates a function object on the spot - โข2. It has no name unless you assign it to a variable
- โข3. The expression after
:is automatically returned โ noreturnkeyword needed - โข4. Commonly passed directly:
sorted(data, key=lambda item: item['score']) - โข5.
map(lambda x: x**2, numbers)applies the function to every item - โข6.
filter(lambda x: x > 0, numbers)keeps only items where the function returnsTrue
Real-World Examples
- โขSort a leaderboard by score:
sorted(players, key=lambda p: p['score'], reverse=True) - โขFilter out inactive users:
list(filter(lambda u: u['active'], users)) - โขDouble all prices:
list(map(lambda p: p * 2, prices)) - โขSort files by size:
sorted(files, key=lambda f: f.size) - โขGUI button callbacks:
btn.command = lambda: print('clicked')
Key Facts
- โขLambdas are function objects โ they can be stored in variables, passed as arguments, and returned from functions
- โขThey cannot contain statements (if/else/for/while are not allowed inside โ only expressions)
- โขPEP 8 advises: if you assign a lambda to a name, just use
definstead โdouble = lambda x: x*2should bedef double(x): return x*2 - โขLambdas shine as the
keyargument forsorted(),min(),max()
Watch Out!
Don't overuse lambdas for complex logic. If your lambda needs an if/else (ternary is OK: lambda x: 'yes' if x > 0 else 'no'), or is longer than one line, replace it with a proper def function โ your future self will thank you.
Remember
Use lambdas for short, throwaway functions โ especially as the key in sorted(). For anything complex, use a named def.
What You Learned
- โขLambda creates an anonymous one-line function:
lambda args: expression - โขBest used as inline key functions for
sorted(),map(),filter() - โขUnlocks: clean sorting by custom keys, compact data transformations, functional programming patterns
Key Facts
- โLambdas are function objects โ they can be stored in variables, passed as arguments, and returned from functions
- โThey cannot contain statements (if/else/for/while are not allowed inside โ only expressions)
- โPEP 8 advises: if you assign a lambda to a name, just use
definstead โdouble = lambda x: x*2should bedef double(x): return x*2 - โLambdas shine as the
keyargument forsorted(),min(),max()
Real-World Examples
Remember
Use lambdas for short, throwaway functions โ especially as the key in sorted(). For anything complex, use a named def.
Quick Quiz
lambda is for?