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

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

Why does this code print [None, None, None, None]?

parchment
  • 4,063
  • 1
  • 19
  • 30

2 Answers2

5

Because print returns None?

So, the print is done (4, 5, 6, 7,) and then the list comprehension is returned ([None, None, None, None])

What you want to do is use join:

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

or use the sep argument of print (I don't have python3 right now but something like this:)

print(*map(inc, counters), sep=', ')
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • tks...well, what does the parameter 'map' function with the prefix '*' in print() function mean ? – derekjhyang Nov 26 '14 at 15:48
  • @user2369034 It is used to [unpack](http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters) the list – fredtantini Nov 26 '14 at 15:50
3

print is a function but it return None that's why you are getting none

Do this

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 print prints i 1, 2, 3, 4, and then each time list comprehension return the output which is None hence None, None, None, None

Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24