Issue
I was wondering if map can be used at all to sum the elements of a list.
assume a = [1, 2, 3, 4]
list(map(sum, a))
will give an error that int object is not iterable
because list wants iterables.
map(sum, a)
is a valid statement but given the object, I do not see an easy way to dereference it.
[map(sum, a)]
will return an object inside the list
this answer states that it should be easy. What am I missing here?
Solution
x = list(map(sum,a))
Is equivalent to
x = []
for i in a:
x.append(sum(i))
Sum needs a iterable to apply sum across. If you see the docs syntax goes this way sum(iterable[, start])
. Since int
is not an iterable you get that error.
Answered By - Bharath M Shetty
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.