string copy of a list

David Allen mda at idatar.com
Mon Apr 16 00:22:24 EDT 2001


In article <slrn9dk6ga.98l.warlock at gargoyle.myth>, "Jim Richardson"
<warlock at eskimo.com> wrote:

> I am stumped, str(listname) returns a string of list, but includes the
> commas and [] that the list would show if printed. I want a string copy of a
> list, is there a simple way I have missed? please? thanks

How about this:

list = ['foo', 'bar', 'baz', 'quux']
import string

stringified = string.join(map(str, list), " ")

stringified is now 'foo bar baz quux'.  You can of course change
the join parameter to whatever you want to use to join the fields together.
The reason that map(str, list) is needed is that the items in the list
may not be strings.  You can't for example do this legally:

string.join([1,2,3,4], " ")
becuase those numbers aren't strings.  So mapping the list with str fixes
that problem.
-- 
David Allen
http://opop.nols.com/
----------------------------------------
"Practice random senselessness and act kind of beautiful." -- James Craig Burley



More information about the Python-list mailing list