[Python-Dev] Retrieve an arbitrary element from a set without removing it (original) (raw)

Stephen J. Turnbull stephen at xemacs.org
Mon Oct 26 08:48:39 CET 2009


"Martin v. Löwis" writes:

res.get() would be a fairly obvious way to do it. Enough that I would probably never have gone searching for any of the other answers. Though personally, I think I would call it "set.peek()", but the specific name doesn't really matter to me.

Somebody proposed to call it .any(); this I like best (except that one might expect that any takes a predicate as the argument).

Why not allow that?

def any(self, predicate=lambda x: True, default=None)
    for a in self:
        if predicate(a):
            break
    else:
        return default
    return a


More information about the Python-Dev mailing list