0

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: enter image description here


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.

3
  • 2
    sum(map(lambda x:x, range(10))) works just fine for me on Python 3.4.2. What version of Python are you running exactly? Commented Apr 14, 2015 at 1:14
  • what's the output of print sum? Commented Apr 14, 2015 at 7:41
  • from builtins import * solved the issue by overwriting the numpy.sum with builtins.sum. Thanks to everybody for your patience and help. Commented Apr 14, 2015 at 16:50

2 Answers 2

3

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Neither of the examples required list() when i can from command line. probably this is an issue with some default imports in ipythonNotebook.
@BiGYaN what's the output of print sum?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.