Printing using list comprehension

后端 未结 10 1732
一生所求
一生所求 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 11:53

    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.

提交回复
热议问题