Python Lambda Functions, Explained Like a Sticky Note
Not every instruction deserves a full-page memo. Sometimes you just scribble a quick note, "buy milk," stick it somewhere, and throw it away once it's done. A lambda function in Python is that sticky note: a tiny, throwaway function for a one-line job that doesn't need a name or a permanent home.
What a Lambda Function Looks Like
A regular function needs a def, a name, and usually a return statement.
A lambda does the same thing in one line, with no name and no return keyword, the return is implied.
double = lambda x: xThe syntax is: lambda parameters: expression. Whatever the expression evaluates to is automatically returned.
Why Not Just Use def Every Time?
You can. But lambdas come in handy when a function is so small and short-lived that naming it seems unnecessary, like writing "buy milk" all over a memo template instead of on a sticky note. This is often the case when you're passing a quick function to another function.
Lambda With sorted()
A classic real-world use is customizing how a list gets sorted.
people = [("Alex", 34), ("Sam", 21), ("Jordan", 45)] people.sort(key=lambda person: person[1])print(people) # sorted by age: [('Sam', 21), ('Alex', 34), ('Jordan', 45)]Here,
sort()→ sorts the list.key=→ tells Python what to use for sorting.lambda person:→ creates a small, temporary function for each person.person[1]→ gets the second value from the tuple, which is the age.- And sorts them from smallest age to largest:
Lambda With map()
map() applies a function to every item in a list. A lambda keeps this compact.
numbers = [1, 2, 3, 4]squared = list(map(lambda n: n ** 2, numbers))print(squared)Here,
lambda n: n ** 2→ Take a number and multiply it by itself.- Then
map()applies this to every number in the list.
Why are we using list() here?
By itself, map() doesn't give you a normal list. It gives you a special map object containing the results.
- You won't get this:
[1, 4, 9, 16] - Instead, Python will display something like this:
<map object at 0x...>, becausemap()creates a map object, not a list. - So we use
list()to convert these results into a normal list.
Lambda With filter()
filter() keeps only the items where the function returns True.
numbers = [1, 2, 3, 4, 5, 6]evens = list(filter(lambda n: n % 2 == 0, numbers))print(evens)Here,
lambda n: n % 2 == 0→ It will check whether the number is even or not.- The
filter()function will check every number and will keep the items that returnTrueand remove the ones that returnFalse.
- Just like
map(),filter() returns a special filter object, not a normal list. So we uselist()to convert the result into a regular list.
Lambda With Multiple Arguments
A lambda can take more than one parameter, separated by commas, just like a regular function.
add = lambda a, b: a + bprint(add(3, 4))When to Avoid Lambdas
Sticky notes are great for quick reminders, but nobody writes novels on them. If your function needs multiple lines, a docstring, or a clear name to make it easier to read, use a regular def function instead. A lambda crammed with complex logic is a common source of unreadable code.
What's Next
Lambdas are small, but they raise a bigger question: what happens to variables inside a function once it finishes running? The next tutorial covers scope, the rules for where a variable lives and who can see it.
Python Functions 7-Part Masterclass:
- Part 1: Python Functions Explained Like a Recipe (Beginner's Guide)
- Part 2: Python Function Parameters and Arguments (Explained Like Ordering Food)
- Part 3: *args and **kwargs in Python Explained Like a Potluck Party
- Part 4: Python Lambda Functions Explained Like a Sticky Note (Current)
- Part 5: Python Variable Scope Explained Like an Office Building
- Part 6: Python Recursion Explained Like Russian Nesting Dolls
- Part 7: Python Closures and Decorators Explained Like Gift Wrapping