Pythonic way to print list items

578,210

Solution 1

Assuming you are using Python 3.x:

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

You can get the same behavior on Python 2.x using from __future__ import print_function, as noted by mgilson in comments.

With the print statement on Python 2.x you will need iteration of some kind, regarding your question about print(p) for p in myList not working, you can just use the following which does the same thing and is still one line:

for p in myList: print p

For a solution that uses '\n'.join(), I prefer list comprehensions and generators over map() so I would probably use the following:

print '\n'.join(str(p) for p in myList) 

Solution 2

I use this all the time :

#!/usr/bin/python

l = [1,2,3,7] 
print "".join([str(x) for x in l])

Solution 3

[print(a) for a in list] will give a bunch of None types at the end though it prints out all the items

Solution 4

For Python 2.*:

If you overload the function __str__() for your Person class, you can omit the part with map(str, ...). Another way for this is creating a function, just like you wrote:

def write_list(lst):
    for item in lst:
        print str(item) 

...

write_list(MyList)

There is in Python 3.* the argument sep for the print() function. Take a look at documentation.

Solution 5

Expanding @lucasg's answer (inspired by the comment it received):

To get a formatted list output, you can do something along these lines:

l = [1,2,5]
print ", ".join('%02d'%x for x in l)

01, 02, 05

Now the ", " provides the separator (only between items, not at the end) and the formatting string '02d'combined with %x gives a formatted string for each item x - in this case, formatted as an integer with two digits, left-filled with zeros.

Share:
578,210
Guillaume Voiron
Author by

Guillaume Voiron

Updated on November 19, 2021

Comments

  • Guillaume Voiron
    Guillaume Voiron over 2 years

    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".join(map(str, myList)))
    Foo
    Bar
    

    I read this way is 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
    

    If not, my question is... why ? If we can do this kind of stuff with comprehensive lists, why not as a simple statement outside a list ?

  • mgilson
    mgilson about 11 years
    And if not, you can from __future__ import print_function on python2.6 and newer.
  • Juan Carlos Moreno
    Juan Carlos Moreno about 11 years
    in python 2x using map is slightly faster than using join on a list comprehension.
  • Andrew Clark
    Andrew Clark about 11 years
    Why the downvote, is it really due to the speed difference between map() and a generator? Python's creator also happens to prefer comprehensions and generators over map().
  • Juan Carlos Moreno
    Juan Carlos Moreno about 11 years
    I didn't down vote you. I am familiar with that post from GVR which was his way of saying, back then, how future versions of python were not going to include it but, they ended up staying.
  • Andrew Clark
    Andrew Clark about 11 years
    They did stay in Python 3.x, but his point in that article is that [F(x) for x in S] is more clear than map(F, S). Performance is not addressed there, but I would expect the speed difference to be negligible. Anyway I was just confused about the downvote, sorry I assumed it was you!
  • rsaw
    rsaw almost 10 years
    Wasn't aware of this feature of python3's print(). Dopealicious. Good thing I've always used new-style print syntax (with parens) in my python2 code. Thanks Andrew.
  • ChrisFreeman
    ChrisFreeman about 8 years
    To add format(), replace the str(x) with a format string: print " ".join(["{:02d}".format(x) for x in l])
  • Eliezer Miron
    Eliezer Miron almost 8 years
    Can this be combined with other strings in a single print statement?
  • BretD
    BretD about 7 years
    Just as a note, please check the answer above you. You use a range, which provides an index value, i, yet you use another variable, indexval, as your index?? You're fighting against the simplicity of python. for val in my_list: print val does the same as what you have
  • alete
    alete over 5 years
    this is what I've been using so far. It's one line, very clear and expresive, but linter complaints "Expression is assigned to nothing"
  • rassa45
    rassa45 over 5 years
    If you want to troll the linter, you can make a separate function that prints it out and returns some stupid value and call that function in the list comprehension
  • Hurricane Development
    Hurricane Development over 5 years
    What is the function of the asterisk in the Python3 print statement?
  • Mahesha999
    Mahesha999 over 3 years
    What does *myList?
  • onxx
    onxx over 2 years
    @AndrewClark on the money!