"Stringizing" a list

Kent Polk kent at tiamat.goathill.org
Wed Aug 9 12:31:23 EDT 2000


On Wed, 09 Aug 2000 16:55:26 +0200, Cezar Ionescu wrote:
>[Cees de Groot]
>> 
>> I'm sure I've seen a place with a snippet for this, but I couldn't find it:
>> 
>> what's the cleanest way to convert a random nested list structure like:
>> 
>> ['foo', ['bar', 'baz'], 'quux']
>> 
>> into:
>> 
>> 'foo bar baz quux' ?

>The result is:
>
>'aaa bbb ccc ddd eee fff ggg'

Is the task just to 'stringify' a list or to represent a list using
a string? I.e. does the list need to be rebuilt at some point?

For example, I'm using xmlrpc to pass data to a server. The data
consists of a list of typically about 6k records (10 element lists)
which amounts to approx 200k of text.  The current xmlrpclib parser
is implemented in Python, so is exceedingly slow on these sorts of
data. Plus, the xml representation of a table (2d lists) becomes
pretty fluffy.  As a result, it takes about 3 minutes to pass this
table to the server and have it rebuild the list. Another way is
to convert the table to a string, send the string as data, and
rebuild the table on the server side. Instead of 3 minutes, it
takes about 4 seconds using the string representation. It's fairly
easy to use string.join and string.split with different separators
for the nested lists to do this. For example, I use newlines for
the record separators and tabs for the item separators. This, of
course, assumes the list data is already a string...




More information about the Python-list mailing list