print is not a function
Peter Otten
__peter__ at web.de
Wed Oct 8 06:50:17 EDT 2003
Karl Scalet wrote:
> quite often there is a need to just print out the items of a list.
>
> [ prt(x) for x in my_list ]
>
> that would be nice, except prt() does not exist, and print is a
> statement not a function, hence cannot replace prt as of above.
>
> I don't like to write d
> def prt(x):
> print x
> beforehand and any lambda construct would not be so handy.
> It should be a short one-liner.
> Any ideas?
How about
>>> myList = "alpha beta gamma".split()
>>> print "\n".join([str(x) for x in myList])
alpha
beta
gamma
This way you at least put the list resulting from the comprehension to some
use.
General rule: use list comprehensions only if you need a list, otherwise for
loops are both more efficient and clearer regarding the code's purpose.
Peter
More information about the Python-list
mailing list