[Python-ideas] data structures should have an .any() method

Guido van Rossum guido at python.org
Fri Sep 4 20:04:34 CEST 2009

On Fri, Sep 4, 2009 at 2:35 AM, Stefan Behnel<stefan_ml at behnel.de> wrote:
> I just had a discussion with a co-worker, and we noticed that there are use
> cases where you just want the only element in a data structure, or just any
> of the elements in a data structure because you know that they all contain
> the same information (with respect to what you are looking for, at least).
> If you want all items, you can iterate, but if you just want any item or
> the only item, it's inefficient (and not very explicit code) to create an
> iterator and take the element out.

I assure you it's not slow. next(iter(x)) is probably as good as it
gets -- I don't think we need another way to say that in fewer words.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-ideas mailing list