Pythonic way to print list items

后端 未结 11 1027
渐次进展
渐次进展 2020-11-22 10:29

I would like to know if there is a better way to print all objects in a Python list than this :

myList = [Person(\"Foo\"), Person(\"Bar\")]
print(\"\\n\".joi         


        
11条回答
  •  难免孤独
    2020-11-22 11:00

    OP's question is: does something like following exists, if not then why

    print(p) for p in myList # doesn't work, OP's intuition
    

    answer is, it does exist which is:

    [p for p in myList] #works perfectly
    

    Basically, use [] for list comprehension and get rid of print to avoiding printing None. To see why print prints None see this

提交回复
热议问题