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

Nick Coghlan ncoghlan at gmail.com
Fri Sep 4 13:50:11 CEST 2009

Matteo Dell'Amico wrote:
> You can do next(iter(container)) (or iter(container).next() with python
> <= 2.5). This works fine with any iterable.

This and other responses miss part of Stefan's complaint: that creating
an iterator (which isn't always cheap) only to throw it away almost
immediately may be a somewhat wasteful operation.

The shorthand expression above also suffers from the obscurity that
Stefan was complaining about - there is very little to hint that
"next(iter(obj))" means "get an arbitrary object out of a container".
The StopIteration exception this approach will throw for an empty
container is also rather unhelpful.

That said, I'm -0 on the idea overall. If someone actually needs it, it
isn't particularly hard for them to write their own getany() function.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list