Back to Blog
Python

Python Lambda Multiple Arguments: Syntax and Use Cases

Learn how to define Python lambdas with multiple arguments, use them with map and reduce, and know when a regular def function is clearer.

lambda functionspython syntaxanonymous functionsfunctional programmingpython built-ins
Illustration of a Python lambda function taking multiple arguments, shown as a funnel with several inputs merging into a single expression.

In Python, a lambda function can accept multiple arguments by separating them with commas in the argument list. The syntax is lambda arg1, arg2, ...: expression. This article explains how to define and use lambdas with multiple arguments in real code, including built-in functions, common pitfalls, and when a regular def is a better choice.

Defining a Lambda with Multiple Arguments

A lambda function is a small anonymous function defined with the lambda keyword. It takes as many arguments as you define, but its body must be a single expression. For example:

add = lambda x, y: x + y print(add(3, 4)) # 7

Here, x and y are the arguments, and x + y is the expression evaluated when the lambda is called. You can also pass more than two arguments:

multiply = lambda a, b, c: a * b * c print(multiply(2, 3, 4)) # 24

The arguments are positional by default, but you can use keyword arguments when calling the lambda:

divide = lambda numerator, denominator: numerator / denominator print(divide(denominator=2, numerator=10)) # 5.0

This works because a lambda's parameter names follow the same rules as a normal function definition.

Using Multiple Arguments in Built-in Functions

Some built-in or standard-library functions call a lambda with more than one argument. map is the most direct example: it can combine two or more iterables element-wise.

list1 = [1, 2, 3] list2 = [10, 20, 30] result = list(map(lambda x, y: x + y, list1, list2)) print(result) # [11, 22, 33]

functools.reduce also takes a two-argument callable:

from functools import reduce numbers = [1, 2, 3, 4] sum_all = reduce(lambda a, b: a + b, numbers) print(sum_all) # 10

filter and the key argument of sorted, by contrast, call their callable with one item at a time. A lambda with multiple arguments is therefore not directly applicable to those callbacks. If you need to adapt a multi-argument function, fix the extra arguments with functools.partial or wrap it in a one-argument lambda.

For example, sorted commonly uses a key function that returns a composite value:

pairs = [(1, 'apple'), (2, 'banana'), (3, 'cherry')] sorted_pairs = sorted(pairs, key=lambda pair: pair[1])

Here the lambda receives a single tuple, not multiple arguments. To unpack a tuple into several arguments, itertools.starmap works well:

from itertools import starmap points = [(1, 2), (3, 4), (5, 6)] sums = list(starmap(lambda x, y: x + y, points)) print(sums) # [3, 7, 11]

starmap unpacks each tuple into separate arguments, so the lambda's multiple parameters work naturally.

Capturing and Evaluating Arguments at Call Time

A lambda captures variables from its enclosing scope, but its expression is evaluated only when the lambda is called. This can lead to surprising behavior in loops if you create lambdas that reference loop variables. For example:

funcs = [lambda x: x + i for i in range(3)] print([f(10) for f in funcs]) # [12, 12, 12] because i is late-bound

To capture the current value, use a default argument:

funcs = [lambda x, i=i: x + i for i in range(3)] print([f(10) for f in funcs]) # [10, 11, 12]

This applies to lambdas with multiple arguments as well. If you need to freeze a value at definition time, bind it as a default parameter.

When a Lambda Becomes Hard to Read

Lambdas are concise, but they can hurt readability when the expression is complex. A lambda body can only contain a single expression, so anything that requires statements, assignments, or multiple steps must be written as a regular function. For example, this lambda is difficult to follow:

result = (lambda a, b: a if a > b else b)(3, 5)

While it works, the intent is clearer with a def:

def max_of_two(a, b): return a if a > b else b

If you find yourself writing a lambda that spans multiple lines or uses nested parentheses, replace it with a named function. A lambda's brevity is only beneficial when the logic is trivial and the call site is immediately clear.

Runtime Behavior and Maintainability

Lambdas are not inherently faster than equivalent def functions. They are regular function objects with the same call overhead. The main tradeoff is maintainability: lambdas are anonymous, so tracebacks and debuggers show <lambda> instead of a meaningful name. This makes errors harder to diagnose.

Because a lambda body must be a single expression, you cannot include statements such as assert or a regular assignment inside one. print(x) is a function call in Python 3, so it is syntactically valid, but using a lambda for side effects usually hurts readability. The walrus operator can perform assignments inside an expression, but side-effect-heavy lambdas are rarely worth the complexity. If you need multiple operations or statements, a def is the correct tool.

When you use lambdas with multiple arguments in a hot path, the overhead is negligible compared to the function call itself. The real cost is often code clarity, not CPU cycles. Profile your application if you suspect lambda usage is a bottleneck; otherwise, prioritize readability.

Practical Patterns for Multiple Arguments

If you have a multi-argument lambda and need a single-argument callback, functools.partial can fix some of the arguments:

from functools import partial add = lambda x, y: x + y add_ten = partial(add, 10) values = [1, 2, 3] print(list(map(add_ten, values))) # [11, 12, 13]

partial returns a callable that remembers some arguments and passes the remaining ones when called. The same idea can adapt a multi-argument predicate for use with filter.

For iterables of tuples, starmap is the cleanest way to pass each item's elements as separate arguments. It avoids creating intermediate tuples or using index-based unpacking.

A practical sort pattern that looks like it needs multiple arguments often does not: a key function can return a tuple as a composite key.

records = [ {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}, ] sorted_records = sorted(records, key=lambda r: (r['age'], r['name']))

Here the lambda receives a single record, not multiple arguments, and the returned tuple gives the sort order. Prefer this when a callback is documented to accept one item, even if the item has several fields.

Compatibility and Version Considerations

Core lambda syntax is stable across Python versions, and itertools.starmap is available in the standard library.

One limitation remains in modern Python: lambda parameters cannot be annotated with type hints using the lambda x: int syntax. If you need type hints and static analysis, use a def function.

For most practical purposes, a lambda with multiple arguments is a tool for short, expression-based logic. When the logic grows beyond a single expression, or when you need documentation and type hints, switch to a named function. The decision should be based on readability and maintainability, not on any perceived performance benefit.

Python Lambda Multiple Arguments: Syntax, Examples, and Pitfalls | RYUSLOG DEV