TypeError: Reduce() of empty sequence with no initial value TypeError: Reduce() of empty sequence with no initial value python-3.x python-3.x

TypeError: Reduce() of empty sequence with no initial value


Nobody answer why there is an error:TypeError: reduce() of empty sequence with no initial value

When the list literal which is second parameter is empty, the error occurs. So if you tryreduce(lambda x, y:(x[0]+y[0], x[1]+y[1]), [])

You'll get the error.


map(sum, ...) fits better, looks beautiful.

map(sum, zip(*mapped))

You can use itertools.izip_longest if lengths of the lists are different.


Use this way:

>>> reduce(lambda x,y:(x[0]+y[0], x[1]+y[1]),[i for i in mapped])(3, 119)>>> reduce(lambda x,y:(x[0]+y[0], x[1]+y[1]),(i for i in mapped))(3, 119)

What you miss is that lambda should take two parameters, you just give one.

For Python3.x see the code below:

>>> from functools import reduce>>> reduce(lambda x:(x[0]+y[0], x[1]+y[1]),(i for i in mapped))Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: <lambda>() takes exactly 1 positional argument (2 given)>>> reduce(lambda x,y:(x[0]+y[0], x[1]+y[1]),(i for i in mapped))(3, 119)