Skip to main content

How to Write a Lambda Function in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some functions are needed only once, as an argument to another function, such as the rule sorted() uses to order a list. Python's lambda writes that kind of function in one line, right where it's used, without a name. This guide sorts numbers by how close they are to 10 on Python 3.15.2.

Syntax​

Syntax
lambda <parameters>: <expression>
PartWhat it means
lambdaThe keyword that starts the function
<parameters>The inputs, separated by commas, as in a def line: n, or a, b
<expression>One expression. Its value is what the function returns, without the word return

The language reference says: "Lambda expressions (sometimes called lambda forms) are used to create anonymous functions." It adds that they "cannot contain statements or annotations": one expression is all they hold.

The Example​

A list of five numbers is sorted twice: first in normal order, then with a lambda as the key, which orders the numbers by their distance from 10.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

closest.py
numbers = [3, 18, 9, 12, 25]

print(sorted(numbers))
print(sorted(numbers, key=lambda n: abs(n - 10)))

The closest.py script in a macOS-style code window: a list of the numbers 3, 18, 9, 12 and 25, printed sorted, then sorted with key=lambda n: abs(n - 10).

  • lambda n: abs(n - 10) takes one number, n, and returns its distance from 10.
  • sorted() calls the function once per number and orders the numbers by what it returns, smallest first.

Step 2: Run It​

Terminal
uv run python closest.py
Output
[3, 9, 12, 18, 25]
[9, 12, 3, 18, 25]

The terminal after uv run python closest.py: the normal order 3, 9, 12, 18, 25, then the order by distance from 10: 9, 12, 3, 18, 25.

The second list is ordered by distance: 9 is 1 away from 10, 12 is 2 away, 3 is 7 away, 18 is 8 away and 25 is 15 away. The numbers themselves didn't change; only their order did. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Writing return inside the lambda: lambda n: return abs(n - 10)SyntaxError: invalid syntax, pointing at returnLeave return out: the expression's value is returned
Putting a statement in it, such as lambda n: distance = abs(n - 10)SyntaxError: invalid syntax, pointing at =Keep it to one expression, or write a def function
Naming a lambda to reuse it: distance = lambda n: abs(n - 10)It works, but PEP 8 says "Always use a def statement instead"Write def distance(n): return abs(n - 10)

Troubleshooting​

SymptomLikely causeFix
SyntaxError: invalid syntax inside a lambdaA statement such as return, = or if ...: is in itUse one expression; for more, write a def
The list isn't sorted the way you expectThe lambda returns a different value than you thinkCall it on one item to check: (lambda n: abs(n - 10))(3) gives 7

FAQ​

lambda or def?​

They make the same kind of function. The tutorial calls lambdas "syntactic sugar for a normal function definition". Use a lambda for a short rule you pass once, as above. Use def for anything you name, reuse or need more than one line for. The same sort with def distance_from_ten(n): return abs(n - 10) and key=distance_from_ten prints the same [9, 12, 3, 18, 25].

Can a lambda take more than one argument?​

Yes, separated by commas: lambda a, b: a + b takes two and returns their sum.

Conclusion​

lambda n: abs(n - 10) was a whole function in one line, and sorted() used it to order the numbers by distance from 10. Keep a lambda to one expression with no return, and switch to def when it needs a name.

Additional Resources​