Skip to content
Advertisement

Tag: sum

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 The working version I wrote using map is How to use reduce then? I wrote: I got the error “TypeError: ‘float’ object is unsubscriptable”. Can anyone shed

Python: Sum string lengths

Is there a more idiomatic way to sum string lengths in Python than by using a loop? I tried sum(), but it only works for integers: Answer I know this is an old question, but I can’t help noting that the Python error message tells you how to do this: So:

How to sum dict elements

In Python, I have list of dicts: I want one final dict that will contain the sum of all dicts. I.e the result will be: {‘a’:5, ‘b’:7} N.B: every dict in the list will contain same number of key, value pairs. Answer A little ugly, but a one-liner:

Advertisement