Splitting a list

Peter Otten __peter__ at web.de
Tue Aug 31 11:08:06 EDT 2004


Jeff Epler wrote:

> Here's my version as a generator function:
>     def split(it, elem):
>         l = []
>         for i in it:
>             if i == elem:
>                 yield l
>                 l = []
>             else:
>                 l.append(i)
>         yield l
> 

A clean algorithm, but it has one corner case that strikes me as
counterintuitive:

>>> list(split([], None))
[[]]

I would prefer [], so I cross-checked with str.split():

>>> "".split()
[]
>>> "".split("x")
['']

Strange. Is that by design or by accident?

Peter





More information about the Python-list mailing list