Splitting a list

Ian Sparks Ian.Sparks at etrials.com
Tue Aug 31 15:54:17 CEST 2004


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 l]).split('-1')]
>> [[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