Error in lambda function..!

Peter Otten __peter__ at web.de
Sat Aug 29 04:59:12 EDT 2020


Shivlal Sharma wrote:

> from functools import*
> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
> add = reduce(lambda a : a + 1, nums)
> print(add)
> 
> error: -
> TypeError                                 Traceback (most recent call
> last) <ipython-input-15-684b5d4fac30> in <module>()
>       1 from functools import*
>       2 nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
> ----> 3 add = reduce(lambda a : a + 1, nums)
>       4 print(add)
> 
> TypeError: <lambda>() takes 1 positional argument but 2 were given

Read the error message: your lambda accepts ony one argument, but reduce 
tries to pass two to it. That you don't need the second argument doesn't 
mean that you can it omit it, so:

>>> from functools import reduce
>>> reduce(lambda a, b: a + 1, range(1, 10))
9




More information about the Python-list mailing list