Ask Your Question

Revision history [back]

The error message indicates that the error is about the lambda function.

That lambda function takes two arguments x and y.

The error message complains that it only received x and not y.

Indeed, the lambda function expects two arguments x and y but receives a single argument which is a tuple (x, y).

Illustration:

sage: f = lambda x, y: x + y
sage: ell = [(1, 2), (3, 4), (5, 6)]

sage: list(map(f, ell))
Traceback (most recent call last)
...
TypeError: <lambda>() missing 1 required positional argument: 'y'

Compare:

sage: f(1, 2)
3

sage: f((1, 2))
Traceback (most recent call last)
...
TypeError: <lambda>() missing 1 required positional argument: 'y'

Use sum, which takes an iterable as argument, instead of the custom f:

sage: list(map(sum, ell))
[3, 7, 11]

Or drop map and use list comprehension.

Either directly

sage: [x + y for x, y in ell]
[3, 7, 11]

Or using f and * for tuple unpacking:

sage: [f(*t) for t in ell]
[3, 7, 11]

The error message indicates that the an error is about with the lambda function.

That lambda function takes two arguments x and y.

The error message complains that it only received x and not y.

Indeed, the lambda function expects two arguments x and y but receives a single argument which is a tuple (x, y)(a, b).

The tuple is not automatically unpacked to give x = a and y = b.

Instead, what happens is x = (a, b) and there is no y, so lambda complains.

Illustration:

sage: f = lambda x, y: x + y
sage: ell = [(1, 2), (3, 4), (5, 6)]

sage: list(map(f, ell))
Traceback (most recent call last)
...
TypeError: <lambda>() missing 1 required positional argument: 'y'

Compare:

sage: f(1, 2)
3

sage: f((1, 2))
Traceback (most recent call last)
...
TypeError: <lambda>() missing 1 required positional argument: 'y'

Use sum, which takes an iterable as argument, instead of the custom f:

sage: list(map(sum, ell))
[3, 7, 11]

Or drop map and use list comprehension.

Either directlydirectly:

sage: [x + y for x, y in ell]
[3, 7, 11]

Or using f and * for tuple unpacking:

sage: [f(*t) for t in ell]
[3, 7, 11]