I previously created a recursive function to find the product of a list.
Now I've created the same function, but using the reduce
function and 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 handle an empty list, is the issue with the reduce function just that it just isn't designed to handle and empty list? or is there another reason?
I cannot seem to find a question or anything online explaining why, I can only find questions with solutions to that particular persons issue, no explanation.