I previously created a recursive function to search for the product of a list. Now I created the same function, but using the reduceand function lamdba.
When I run this code, I get the correct answer.
items = [1, 2, 3, 4, 10]
print(reduce(lambda x, y: x*y, items))
However, when I give an empty list, an error occurs - reduce() of empty sequence with no initial value. Why is this?
When I created my recursive function, I created code to process an empty list, the problem with the reduction function is simply that it is simply not designed to process an empty list? or is there another reason?
It seems that I can not find any question or something on the network that explains why, I can only find answers to solutions to this problem, no explanation.
Chris source
share