Splitting text at whitespace but keeping the whitespace in thereturned list

Roy Smith roy at panix.com
Tue Jan 26 07:40:15 EST 2010


In article <hjklfd$llm$1 at foggy.unx.sas.com>,
 "Tim Arnold" <tim.arnold at sas.com> wrote:

> also, partition works though it returns a tuple instead of a list.
> >>> s = 'hello world'
> >>> s.partition(' ')
> ('hello', ' ', 'world')

I've never used partition() before; my first thought on reading the above 
was, "That's weird, it should be returning a list".  Then I went and looked 
at the docs.  Given the description (returns specifically a 3-tuple), I 
guess a tuple makes sense, but now I'm wondering what the use case was for 
this method when it was invented?

Having a variant of split() which either leaves the delimiter on the end of 
each word, or returns a list of alternating [word, delimiter, word, 
delimiter, word] seems logical and orthogonal.  In fact, partition() is 
really just the hypothetical whitespace-preserving variant of split(), with 
maxsplit=1, except that it returns a tuple instead of a list.

So, what was the original problem partition() was trying to solve?



More information about the Python-list mailing list