Skip to content
Advertisement

Python how to reduce multiple lists?

I am able to use map and sum to achieve this functionality, but how to use reduce?

There are 2 lists: a, b, they have same number of values. I want to calculate

JavaScript

The working version I wrote using map is

JavaScript

How to use reduce then? I wrote:

JavaScript

I got the error “TypeError: 'float' object is unsubscriptable“.

Can anyone shed some light on this?

Advertisement

Answer

The first argument of the lambda function is the sum so far and the second argument is the next pair of elements:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement