Apollo
Apollo

Reputation: 9064

Printing using list comprehension

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?

Upvotes: 38

Views: 61542

Answers (14)

flywire
flywire

Reputation: 1375

Alex Taylor gave the correct answer.

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.

The None output can be consumed:

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

Upvotes: 0

Enmanuel Verdesia
Enmanuel Verdesia

Reputation: 617

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.

Upvotes: 0

Jewel
Jewel

Reputation: 49

[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

Upvotes: 3

xiaoou wang
xiaoou wang

Reputation: 1041

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

Upvotes: 0

Michael H.
Michael H.

Reputation: 615

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

Upvotes: 0

Brendan Metcalfe
Brendan Metcalfe

Reputation: 803

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

Upvotes: -1

Allen Philip Abraham
Allen Philip Abraham

Reputation: 31

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]

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49330

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

Upvotes: 47

Dr. Younes Henni
Dr. Younes Henni

Reputation: 1771

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.

Upvotes: 7

Wayne Yao
Wayne Yao

Reputation: 11

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

Upvotes: 1

Pythoner
Pythoner

Reputation: 5595

print is a function, it's just like

>>>def f(x):
...:   pass
>>>[f(x) for x in numbers]

Upvotes: 1

Óscar López
Óscar López

Reputation: 236140

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)

Upvotes: 22

Alex Taylor
Alex Taylor

Reputation: 8853

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.

Upvotes: 3

awesoon
awesoon

Reputation: 33701

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.

Upvotes: 2

Related Questions