Splitting lists

Létezõ letezo at fw.hu
Thu Feb 27 17:05:01 CET 2003

Dear List Splitters!

Thanks to Steven for the performance analysis and Alex & Duncan for the
their more impressive solutions. It would be interesting to insert the new
"impressive" solutions into the performace test loop. Which is the fastest?

Proposed solutions in reverse time order:

# Duncan modified by Viktor: (one name lookup moved out of the loop)
tl = [ x for x in test if fn(x) or app(x) ]

# Duncan: (my current favourite)
tl = [ x for x in test if fn(x) or fl.append(x) ]

# Alex: (it's a good idea to move the lookups out)
appenders = tl.append, fl.append
for e in lst: appenders[not fn(e)](e)

# Viktor 1
tl=[e for e in lst if fn(e)]
fl=[e for e in lst if not fn(e)]

# Viktor 2
for e in lst:
    if fn(e): tl.append(e)
    else: fl.append(e)

# Viktor 3
cl=[(e,fn(e)) for e in lst]
tl=[e[0] for e in cl if e[1]]
fl=[e[0] for e in cl if not e[1]]

I would be useful to implement a sequence method to do this job at maximum
performance. It would be a "readibility improvement" such as enumerate() in
Python 2.3.

Best wishes: Viktor

More information about the Python-list mailing list