Splitting a list

Martin DeMello martindemello at yahoo.com
Wed Sep 1 03:14:39 EDT 2004


Ian Sparks <Ian.Sparks at etrials.com> wrote:
> string.split() is very useful, but what if I want to split a list of integers on some element value?
> 
>  e.g. :
> 
> >> l = [1,2,3,-1,4,5,-1,8,9]
> >> l.split(-1)
> >> [[1,2,3],[4,5],[8,9]]

reduce (lambda a, i: i == elem and (a + [[]]) or (a[:-1] + [a[-1]+[i]]),
l, [[]])

martin



More information about the Python-list mailing list