Should any() and all() take a key= argument?

Steve R. Hastings steve at hastings.org
Sat Apr 1 01:35:10 EST 2006


The list.sort() method accepts a "key=" parameter to let you specify a
function that will change the way it sorts.  In Python 2.5, min() and
max() now accept a "key=" parameter that changes how the functions decide
min or max.

Should any() and all() take a key= argument?

Example:

>>> lst = [2, 4, 42]
>>> any(lst, key=lambda x: x == 42)
True
>>> all(lst, key=lambda x: x % 2 == 0)
True

The above could be done with generator expressions:

>>> any(x == 42 for x in lst)
True
>>> all(x % 2 == 0 for x in lst)
True


I kind of like the key= option.  The need isn't as strong as with
.sort(), min(), and max(), but consistency can be a good thing.  I'd
personally like to see key= anywhere it makes sense.
-- 
Steve R. Hastings    "Vita est"
steve at hastings.org    http://www.blarg.net/~steveha




More information about the Python-list mailing list