Splitting lists

Anton Vredegoor anton at vredegoor.doge.nl
Thu Feb 27 10:23:22 EST 2003


On Thu, 27 Feb 2003 04:10:17 +0100, "Ferenczi Viktor" <cx at cx.hu>
wrote:

>I need something like: tl,fl=lst.split(fn)

It's possible to subclass builtin types.
See some example code below.

Regards,
	Anton.

class splittable(list):
    
    def split(self,fn):
        tl = splittable()
        fl = splittable()
        for x in self:
            if fn(x):
                tl.append(x)
            else:
                fl.append(x)
        return tl,fl

def test():
    lst=splittable(range(10))
    def fn(x):
        return x<5
    tl,fl = lst.split(fn)
    print tl,fl

if __name__=='__main__':
    test()






More information about the Python-list mailing list