Opposite of split

Roy Smith roy at panix.com
Sun Aug 15 19:58:54 EDT 2010


In article <4c687936$0$11100$c3e8da3 at news.astraweb.com>,
 Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:

> On Sun, 15 Aug 2010 12:10:10 -0700, Steven Howe wrote:
> 
> >> Strings have a join method for this:
> >>     '\t'.join(someList)
> >>
> >> Gary Herron
> > or maybe:
> > ----------------------------------------- 
> > res = ""
> > for item in myList:
> >      res = "%s\t%s" %  ( res, item )
> 
> 
> Under what possible circumstances would you prefer this code to the built-
> in str.join method?
> 
> Particularly since the code isn't even correct, as it adds a spurious tab 
> character at the beginning of the result string. 

I think you answered your own question.  The possible circumstance would 
be a "find the bug" question on a programming interview :-)  Actually, 
there is (at least) one situation where this produces the correct 
result, can you find it?

The other problem is that the verbose version is O(n^2) and str.join() 
is O(n).



More information about the Python-list mailing list