>> T = map(print, [1, 2, 3]) >>> type(T) ...">

Why does map (print, x) return a list of values ​​"No"?

In Python 3.5, the code is

>>> T = map(print, [1, 2, 3])
>>> type(T)
<class 'map'>

      

returns a map object. I would expect this T map object to contain the numbers 1, 2, and 3; all on separate lines. This is actually happening. The only problem is that it also outputs a list of values None

the same length as the input list.

>>> list(T)
1
2
3
[None, None, None]
>>> 

      

This repeats for any input I use, not just any integer list shown above. Can anyone explain why this is happening?

+3


source to share


1 answer


See also:
fooobar.com/questions/159135 / ...
fooobar.com/questions/306178 / ...
fooobar.com/questions/306180 / ...

Each None

one you see is a function that print

returns . To see what it does map

, try the following code:

>>> T = map(lambda x: x**2, [1, 2, 3])
>>> t = list(T)
>>> print(t)
[1, 4, 9]

      

If you use instead print

:



>>> T = map(print, [1, 2, 3])
>>> t = list(T)
1
2
3
>>> print(t)
[None, None, None]

      

This is not surprising because:

>>> a = print("anything")
anything
>>> print(a)
None

      

+2


source







All Articles