need help with list/variables

5lvqbwl02 at sneakemail.com 5lvqbwl02 at sneakemail.com
Tue Dec 30 14:41:47 EST 2008


On Dec 30, 11:31 am, wx1... at gmail.com wrote:
> 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"""
>
> how would i go about setting a variable like this?

I think you want to concatenate the three elements in your list and
then assign the resulting string to myvar.  Strings that are defined
with tripple quotes still get the newline character as though they
were defined with double or single quotes and \n in them.

>>> conc = lambda x,y: x[:] + y # concatenate 2 lists without side effects
>>> mylist = ['something\n', 'another something\n', 'something again\n']
>>> myvar = reduce(conc, mylist)
>>> print myvar
something
another something
something again

Hope this helps
michael



More information about the Python-list mailing list