bli
bli

Reputation: 8194

Intended effect of sum over a python map

sum(map(len, ["a", "aa", "aaa"])) gives me a map instead of a number.

I expected this to give the same result as sum(len(thing) for thing in ["a", "aa", "aaa"]) (that is 6).

I see that list(sum(map(len, ["a", "aa", "aaa"]))) returns me [1, 2, 3], as if the sum had no effect.

I assume there is a reason for such a behaviour. Is there an intended use case for this?

Upvotes: 3

Views: 436

Answers (1)

bli
bli

Reputation: 8194

It appears that I was working in ipython and that the builtin sum function was masked by "function sum in module numpy.core.fromnumeric" (according to help(sum)). I suspect an effect of having issued the command %pylab.

__builtin__.sum(map(len, ["a", "aa", "aaa"])) gives the expected 6.

Upvotes: 2

Related Questions