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?
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?
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')
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)
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.
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.
[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
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.
print is a function, it's just like
>>>def f(x):
...: pass
>>>[f(x) for x in numbers]
An ugly way of doing this is _=[print(i) for i in somelist]
It does work but is not encouraged:)
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]
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
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
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.
3 ways to print using list comps:
print([(i) or i for i in range(4)])
def printVal(val): print("val: ", val) return val
[printVal(i) or i for i in range(4)]
[print(i) or i for i in range(4)]