newbie question: convert a list to one string

M

martijn

H!

I'm searching for the fastest way to convert a list to one big string.

For example:
test = ['test','test2','test3']

print unlist(test)
test test2 test3 (string)


I know I can make a loop like below but is that the fastest/best option
?

def unlist(test):
output=''
for v in test:
output = output+" "+v
return output


Thanks for helping,
GC-Martijn
 
R

Robert Kern

H!

I'm searching for the fastest way to convert a list to one big string.

For example:
test = ['test','test2','test3']

print unlist(test)
test test2 test3 (string)



I know I can make a loop like below but is that the fastest/best option
?

def unlist(test):
output=''
for v in test:
output = output+" "+v
return output

' '.join(test)

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
S

Sion Arrowsmith

I'm searching for the fastest way to convert a list to one big string.

The join() method of strings. The string instance in question being
the separator you want, so:

" ".join(test)
 

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
474,264
Messages
2,571,315
Members
47,996
Latest member
LaurenFola

Latest Threads

Top