Reputation: 7159
Here are two examples:
sum(list(map(lambda x:x,range(10))))
and
sum(range(10))
The second example does not require a list()
, but the first one does. Why?
How do I know when is list()
a necessity? Similarly using list()
for min()
and max()
.
I am running python 3.3.5 with ipython 2.2.0. Here is what I see:
print(sum)
results in <built-in function sum>
from python console and <function sum at 0x7f965257eb00>
from ipythonNotebook. Looks like an issue with hidden imports in notebook.
Upvotes: 0
Views: 164
Reputation: 64328
Neither of the examples require the use of list
. The sum
builtin function works with any iterable, so converting the result of map
to a list isn't necessary.
Just in case, make sure you are indeed using the builtin sum
function. Doing something like from numpy import *
would override that. (you can simply print sum
and see what you get).
Upvotes: 3
Reputation: 1999
I guess the 1st one just enforces and expects the output of the map function to be a list because if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables.
But either way base on your example, it would still work.
Upvotes: 1