[Python-Dev] Fwd: summing a bunch of numbers (or "whatevers")

Jack Diederich jack@performancedrivers.com
Tue, 22 Apr 2003 22:38:23 -0400


On Tue, Apr 22, 2003 at 09:35:06PM -0400, Greg Ward wrote:
> On 21 April 2003, Tim Peters said:
> > filter() is hard to get rid of because the bizarre filter(None, seq) special
> > case is supernaturally fast.  Indeed, time the above against
> 
> Hmmm, a random idea: has filter() ever been used for anything else?
> I didn't think so.  So why not remove everything *except* that handy
> special-case: ie. in 3.0, filter(seq) == filter(None, seq) today, and
> that's *all* filter() does.

Most frequently I test truth of a member of a tuple or list,
newl = filter(lambda x:x[-2], l)

secondly just plain truth, but here are some other examples.

sql_obs = filter(lambda x:isinstance(x, SQL), l)
words = filter(lambda x: x[-1] != ':', words) # filter out group: related: etc
pad_these = filter(lambda x:len(x) < maxlen, lists)
files = filter(lambda x:dir_matches(sid, x), os.listdir(libConst.STATE_DIR + '/'))
delete_these = map(lambda x:x[0][2:], filter(lambda x: x[1], d.iteritems()))
files = filter(lambda x:x.endswith('.state'), os.listdir(base_dir))

Go ahead, ask why we don't yank out lambda too, nobody uses that *wink*

-jack