Why enum comprehension with map () function in python can return None

>>> counters = [1,2,3,4]
>>> 
>>> [print(i, end=', ') for i in map(inc, counters)]

4, 5, 6, 7, [None, None, None, None]

      

Why [None, None, None, None]

does this code print ?

+3


source to share


2 answers


Because it print

returns None

?

So, printing is done ( 4, 5, 6, 7,

) and then list comprehension ( [None, None, None, None]

) is returned

What you want to do is use join:



>>> ', '.join(str(i) for i in map(inc, counters))
'4, 5, 6, 7'

      

or use the argument sep

to print
(I don't have python3 right now, but something like this :)

print(*map(inc, counters), sep=', ')

      

+5


source


print is a function, but it returns None

why you don't get

Do it



In [3]: [i for i in map(abs, counters)]
Out[3]: [1, 2, 3, 4]

In [7]: a = print(1234)
1234

In [11]: print(a)
None

[print(i, end=', ') for i in map(inc, counters)]

      

so when you do this it print

prints i

1, 2, 3, 4 , and then each time it list comprehension

returns the result which is None

hence No, None, None, None

+3


source







All Articles