Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to know if there is a more reliable way to display all objects in a Python list than this :

myList = [Person("Foo"), Person("Bar")]

print("\n".join(map(str, myList)))

Foo

Bar

I have done in this way which not really good :

myList = [Person("Foo"), Person("Bar")]

for p in myList:

    print(p)

Isn't there something like :

print(p) for p in myList

1 Answer

0 votes
by (108k points)

I am assuming that you are working with Python 3.x version:

print(*myList, sep='\n')

You can implement the same thing on Python 2.x using from __future__ import print_function.

If you want to learn python then do check out the below python tutorial video for better understanding:

Related questions

0 votes
4 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...