Python Forum
printing a list contents without brackets in a print statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: printing a list contents without brackets in a print statement (/thread-24456.html)



printing a list contents without brackets in a print statement - paracelx - Feb-15-2020

Using python 3 I am trying the following code. I would like to change the last two lines of the code where the print(*uninvlist, sep=", " is in the directly above that lines print statement. That way its all in one line. How would I code that where the sep wouldn't error it out. It works fine as a separate line. Im just doing some learning exercises in a book and would like to go a little further with it.

uninvlist = [] #created a empty list that I can add too
uninvlist.insert (0, uninvited_guest.title()) #inserting the first one
uninvlist.insert (-1, seconduniv.title())
uninvlist.insert (-1, thirduniv.title())
uninvlist.insert (-1, forthuniv.title())
uninvlist.sort() #sorts the list
#uninvlist = (uninvited_guest) + (firstuniv) + (seconduniv) + (thirduniv) + (forthuniv) #runs it as one word
#messulist = uninvlist #adding the list to a message
print('\nThe entire uninvited list for the party is as follows:')# + (univmessage)) 
print(*uninvlist, sep=", ") # removed the [] had to put on a separate line for now



RE: printing a list contents without brackets in a print statement - Larz60+ - Feb-15-2020

use join
example:
>>> mlist = ['one', 'two', 'five', 'six']
>>> print(f"{' '.join(mlist)}")
one two five six
>>>