making a string copy of a list

Daniel Klein danielk at aracnet.com
Sat Jan 13 11:39:42 EST 2001


On Fri, 12 Jan 2001 22:53:04 -0800, warlock at eskimo.com (Jim Richardson) wrote:

>OK, I swear I've looked for this, but how do I make a copy of a list, into a
>string, complete with commas seperating the entries in the list? 

The simplest way would be

	s = str(mylist)[1:-1] # removes the enclosing square brackets

This does not remove brackets, parenthesis or curly braces from members inside
the list. For instance,

>>> mylist = ['dead','parrot']
>>> print str(mylist)[1:-1]
'dead', 'parrot'
>>> mylist = [['dead','parrot'], (1, 2, 3), {1:'foo', 2:'bar'}]
>>> print str(mylist)[1:-1]
['dead', 'parrot'], (1, 2, 3), {2: 'bar', 1: 'foo'}

Daniel Klein




More information about the Python-list mailing list