[New-bugs-announce] [issue6379] Add first() function that would take first matching case from an iterator

Zbigniew Braniecki report at bugs.python.org
Mon Jun 29 23:20:26 CEST 2009


New submission from Zbigniew Braniecki <zbigniew.braniecki at gmail.com>:

Python has a very useful function any() that will iterate over a list
and break on the first matching case.

Now, it would be great to have similar function but with:
a) ability to use a callback for matching test
b) that would return the first match

so something like:

key = first(['es-AR','es','en-US'], lambda x:self._value.has_key(x))

instead of:

for k in ['es-AR','es','en-US']:
    if self._value.has_key(k):
        key = k
        break

It would be also nice to be able to capture the first match and return it:
value = first(['es-AR','es','en-US'], lambda x:self._value[x])

I'm not sure which one is more generically applicable.

----------
components: None
messages: 89879
nosy: gandalf
severity: normal
status: open
title: Add first() function that would take first matching case from an iterator
versions: Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6379>
_______________________________________


More information about the New-bugs-announce mailing list