String concatenation - which is the fastest way ?

Chris Angelico rosuav at
Wed Aug 10 14:32:06 CEST 2011

On Wed, Aug 10, 2011 at 12:17 PM,  <przemolicc at> wrote:
> Hello,
> I'd like to write a python (2.6/2.7) script which connects to database, fetches
> hundreds of thousands of rows, concat them (basically: create XML)
> and then put the result into another table. Do I have any choice
> regarding string concatenation in Python from the performance point of view ?
> Since the number of rows is big I'd like to use the fastest possible library
> (if there is any choice). Can you recommend me something ?

First off, I have no idea why you would want to create an XML dump of
hundreds of thousands of rows, only to store it in another table.
However, if that is your intention, list joining is about as efficient
as you're going to get in Python:

lst=["asdf","qwer","zxcv"] # feel free to add 399,997 more list entries

This sets xml to '<foo>asdf</foo><foo>qwer</foo><foo>zxcv</foo>' which
may or may not be what you're after.


More information about the Python-list mailing list