Splitting lists

Steven Taschuk staschuk at telusplanet.net
Thu Feb 27 02:01:51 EST 2003


Quoth Ferenczi Viktor:
> Are there any fast, simple and elegant method to split a list by a filter
> function?

There's nothing built-in afaik; the closest thing is filter.

> My current solutions with their shortcomings:
  [...]
> # Solution 2:
> tl=[]
> fl=[]
> for e in lst:
>     if fn(e): tl.append(e)
>     else: fl.append(e)
> # Relatively slow

Slow relative to what?  It's the fastest of your three solutions:

    Solution    Small List    Big List
       1           0.27         0.21
       2           0.21         0.18
       3           0.33         0.26

(In the first column, each solution was run 1000 times on a
10-element list; in the second, 10 times on a 1000-element list.)

Luckily, solution 2 is also the only one which spins the list just
once, not to mention the simplest and clearest, imho.

An improvement is possible by moving one of the name lookups out
of the loop (code below):

     1lookup       0.17         0.13

And, for hotspots with specific conditions, inlining the condition
is worthwhile:

     inline        0.14         0.10

Code:

    # 1lookup
    addt = tl.append
    addf = fl.append
    for e in lst:
        if fn(e):
            addt(e)
        else:
            addf(e)

    # inline
    for e in lst:
        if e < 5:
            tl.append(e)
        else:
            fl.append(e)

-- 
Steven Taschuk                            staschuk at telusplanet.net
Every public frenzy produces legislation purporting to address it.
                                           (Kinsley's Law)





More information about the Python-list mailing list