need help with list/variables

Tim Chase python.list at
Tue Dec 30 20:51:53 CET 2008

> I have a list and would like to parse the list appending each list
> item to the end of a variable on a new line.
> for instance
> mylist = ['something\n', 'another something\n', 'something again\n']
> then parse mylist to make it appear in my variable in this format:
> myvar = """
> something
> another something
> something again"""

Just use the join() method on an empty string:

   myvar = "".join(mylist)

(though your example would need to be tweaked to make the 
newlines match the newlines in your result, given the newlines in 
your list)

myvar = """something
another something
something again

Or you can do something like

  myvar = '\n'.join(s.rstrip('\n') for s in mylist)

Mix and match to get your desired output.


More information about the Python-list mailing list