32

From my Python console

>>> numbers = [1,2,3]
>>> [print(x) for x in numbers]
1
2
3
[None, None, None]

Why does this print three none's at the end?

Apollo
  • 8,874
  • 32
  • 104
  • 192
  • 6
    because `print()` is a function in python3 that returns `None` – Joran Beasley May 07 '16 at 03:42
  • 1
    The Python interpreter always prints the value of any expression that you enter. If you changed it to `r = [print(x) for x in numbers]`, you would see that `r` now contains `[None, None, None]`. – Tom Karzes May 07 '16 at 03:51

13 Answers13

45

You should restructure your loop to send arguments to print():

>>> numbers = [1,2,3]
>>> print(*(x for x in numbers), sep='\n')

Note that you don't need the explicit generator. Just unpack the list itself:

>>> numbers = [1,2,3]
>>> print(*numbers, sep='\n')
Community
  • 1
  • 1
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • This is faster than for loops `toplevel_Search = glob("*.py") recursive_search = glob("**/*.py",recursive=True) begin = time.time() print (*toplevel_Search,sep="\n") end = time.time() print("Time taken without a loop: ", end-begin) begin = time.time() for i in toplevel_Search: print(i) end = time.time() print("Time taken with a loop: ", end-begin)` Output:: `pathlibDemo.py main.py pathlibDemo_COPY.py globDemo.py Time taken without a loop: 0.00017619 pathlibDemo.py main.py pathlibDemo_COPY.py globDemo.py Time taken with a loop: 0.0001959` – The Owl Sep 14 '21 at 05:45
20

A list comprehension is not the right tool for the job at hand. It'll always return a list, and given that print() evaluates to None, the list is filled with None values. A simple for loop works better when we're not interested in creating a list of values, only in evaluating a function with no returned value:

for x in numbers:
    print(x)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
7

List comprehensions always return a list.

Based on this information, your print() statement must wrap the whole list comprehension argument:

Numbers = [1, 2, 3]

print([x for x in Numbers])

If you want to print items of a list one by one, a for loop is more suitable for this matter.

Dr. Younes Henni
  • 1,613
  • 1
  • 18
  • 22
3

The print function returns None. The list comprehension is applying the function to each element of the list (outputting the results), but collecting those None objects into the results array. The Python interpreter is printing that array.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
3

[print(x) for x in numbers] is a list without values. so it returns none. you can simply do the followings:

print(*numbers)

1 2 3 or

for x in numbers:
 print(x)

1 2 3

Jewel
  • 49
  • 3
2

print is a function in Python 3, which returns a None. Since you are calling it three times, it constructs a list of three None elements.

awesoon
  • 32,469
  • 11
  • 74
  • 99
1

print is a function, it's just like

>>>def f(x):
...:   pass
>>>[f(x) for x in numbers]
Pythoner
  • 5,265
  • 5
  • 33
  • 49
1

An ugly way of doing this is _=[print(i) for i in somelist] It does work but is not encouraged:)

Wayne Yao
  • 11
  • 2
0

If this behaviour of the Print function bothers, I suggest you don't use the Print function in the list comprehension, just use the following one-liner which is compact and more Pythonic:

>>> [x for x in [1,2,3]]
[1, 2, 3]
0

You can use the String Join() method and print the string. For example:

>>> print('\n'.join(numbers))
1
2
3
>>> print(', '.join(numbers))
1, 2, 3
>>> print(',\n'.join(numbers))
1,
2,
3
Michael H.
  • 535
  • 6
  • 11
0

For completeness, the * operator can be used also in combination with f-Strings

print(*(f'this is {x}' for x in [1,2,3]), sep='\n')

this is 1
this is 2
this is 3
xiaoou wang
  • 891
  • 10
  • 13
0

You get a list of None values because print function returns None and list comprehension uses the value of the expressions, not what is printed.

However, you can use the next code (with caution) to obtain the desired behavior inside the list comprehension:

>>> [i for i in range(3) if print(i) or True]
0
1
2
[0, 1, 2]

The i-values are printed when is evaluated the if statement. This if guarantees call the print function and return True, so it doesn't discriminate values from the list.

svex99
  • 518
  • 4
  • 10
-1

3 ways to print using list comps:

  1. print outside

print([(i) or i for i in range(4)])

  1. create a function

def printVal(val): print("val: ", val) return val

[printVal(i) or i for i in range(4)]

  1. Use 'or'

[print(i) or i for i in range(4)]

Brendan Metcalfe
  • 753
  • 10
  • 10