need help with list/variables

W

wx1234

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?
 
5

5lvqbwl02

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
 
T

Tim Chase

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.

-tkc
 
A

Albert Hopkins

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?

myvar = ''.join(mylist)

Though if you really want an "blank line" in the beginning you'll need
to prepend a '\n'

-a
 
J

Jonathan Gardner

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

"conc"? "side effects"? Missing Lisp much? ;-)

Let's try to Pythonize your lisp.

One:

Assigning a lambda to a variable? Just use def. It's pretty much the
same thing.
>>> def conc(x,y): return x[:]+y


Two:

I don't think x+y affects x at all when x and y are lists. (Is that a
lispism?) So x[:]+y has an unnecessary array copy.


Three:

Python may still be slow at string concatenation. (Perl is fast.)
Rather than concatenating one at a time, it's better, I understand, to
build up a list and then join() them together.

The other posters got the right answer in the pythonic way.
 
J

John Machin

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

OTOH escapees from FORTRAN would prefer something like:

TOT = ''
for I in xrange(len(MYLIST)):
TOT = TOT + MYLIST
FTNWRITE(6, '*', TOT)

SNOpy, anyone?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top