Splitting a list

Elaine Jackson elainejackson7355 at home.com
Tue Aug 31 20:10:13 CEST 2004

def indices(x,y):
    if y in x:
        i = x.index(y)
        j = i+1
        return [i]+[z+j for z in indices(x[j:],y)]
    return []

def listSplit(x,y):
    z = [-1] + indices(x,y) + [len(x)]
    return [x[z[i]+1:z[i+1]] for i in range(len(z)-1)]

"Ian Sparks" <Ian.Sparks at etrials.com> wrote in message
news:mailman.2673.1093960462.5135.python-list at python.org...
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]]

Here's my hideous first pass :

>> [[int(z) for z in x.split(',') if z] for x in ','.join([str(a) for a in
>> [[1, 2, 3], [4, 5], [8, 9]]

When I see code like that I just know I've missed something obvious....

More information about the Python-list mailing list