[Tutor] Print elements of list with newlines

Alan Gauld alan.gauld at yahoo.co.uk
Fri Oct 22 13:32:59 EDT 2021


On 22/10/2021 15:58, Julius Hamilton wrote:
> Hey,
> 
> Is there any way to print the elements of a list one by one on new lines,
> in a brief single line of code, as opposed to:
> 
> for item in list:
>   print(item)

The most general way is to generate a string with
newlines(or whatever) in and print the string:

print( '\n'.join(theList))

Thesimplest way for this specific case is to
use list unpacking:

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


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list