Printing using list comprehension

后端 未结 10 1719
一生所求
一生所求 2020-12-03 10:58

From my Python console

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

Why does this print

10条回答
  •  庸人自扰
    2020-12-03 12:03

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

提交回复
热议问题