[Python-Dev] "funny".split("")

Brian Quinlan brian@sweetapp.com
Tue, 12 Mar 2002 10:23:20 -0800


Christian wrote: 
> I'm just translating the Python Pocked Reference the
> second time, and I stumbled over this:
> 
>    "funny".split("")
> 
> gives a ValueError: empty separator.
> 
> Why this?
> I would expect
> 
>    ['f','u','n','n','y']

Why wouldn't you expect this: 
['', 'f', 'u', 'n', 'n', 'y', '']

As with:
>>> ' f u n n y '.split(' ')
['', 'f', 'u', 'n', 'n', 'y', '']

> as result, since this is the maximum result of undoing
> 
>    "".join(['f','u','n','n','y'])
> 
> For what reason is this asymmetry?

There will always be asymmetry because there are many lists that, when
joined by the empty string, return in the same string e.g.

>>> ''.join(['fun', 'ny'])
'funny'
>>> ''.join(['', 'f', 'u', 'n', 'n', 'y', ''])
'funny'
>>> ''.join(['f', 'u', 'n', 'n', 'y'])
'funny'

But the split method can only return one list.

Cheers,
Brian