a_list.count(a_callable) ?

Ping ping.nsr.yeh at gmail.com
Thu Jun 14 21:53:38 CEST 2007


Hi,

I'm wondering if it is useful to extend the count() method of a list
to accept a callable object?  What it does should be quite intuitive:
count the number of items that the callable returns True or anything
logically equivalent (non-empty sequence, non-zero number, etc).

This would return the same result as len(filter(a_callable, a_list)),
but without constructing an intermediate list which is thrown away
after len() is done.

This would also be equivalent to
n = 0
for i in a_list:
    if a_callable(i):  n += 1
but with much shorter and easier-to-read code.  It would also run
faster.

This is my first post and please bear with me if I'm not posting it in
the right way.

Regards,
Ping




More information about the Python-list mailing list