[Tutor] Pythonese/Efficiency/Generalese critique please

Kent Johnson kent37 at tds.net
Sun Jun 5 11:47:13 CEST 2005


Lee Cullens wrote:
> Well, I've tried both and can't produce what I was doing with the for  
> construct.
> With a list    pl = ['a', 'b', 'c', 'd'] of the path components I'm  
> trying to add justified non repeated path elements, say pl[2] and pl [3] 
> to csvline so that csvline would end up '"",'*x plus '"c","d",'
> 
>  >>> pl = ['a', 'b', 'c']
>  >>> csvl = '"",'
>  >>> csvl += '"'.join(pl[0:2]) + '",'
>  >>> csvl
> '"",a"b",'
> 
> which is intended to be '"","a","b",'

OK, let's try to get this right.

Given a list of strings and a divider string, join() returns the strings from the list 'joined' by the divider string. For example,
 >>> pl = ['a', 'b', 'c']
 >>> '","'.join(pl)
'a","b","c'

The syntax for this takes a little getting used to; join() is actually a string method; you call it on the divider string and pass it the list of strings to be joined.

To make a list of quoted, comma separated values, you also need initial and final quotes:
 >>> '"' + '","'.join(pl) + '"'
'"a","b","c"'

In your example you need '","'.join(pl[0:2])

 > Saving one line of code in this module doesn't mean much,  but if it  
> were a efficiency in processing it would?

Conventional wisdom is that it is more efficient to use the join method. More enlightened wisdom says, don't optimize until you know you have a problem, and the only way to know what is fastest in your program with your data is to test. And of course working code always beats broken code :-)

I did some experiments with these alternatives and concluded that if the length of the resulting string is less than about 500-800, the version using += is faster than the one using join():
http://www.pycs.net/users/0000323/weblog/2004/08/27.html

So, it is good to know about join(), but write the code the way that is clearest to you.

Kent





More information about the Tutor mailing list