
2009/9/5 Steven D'Aprano <steve@pearwood.info>:
On Fri, 4 Sep 2009 10:01:28 pm Matteo Dell'Amico wrote:
Why? next(iter(obj)) means, pretty explicitly to me, "iterate on obj and give me one element".
To me, it says "give me the first element", not "give me any (an arbitrary) element" or "give me a random element".
The original use as described was for picking an "arbitrary" element, because all of the elements were effectively the same - "any of the elements in a data structure because you know that they all contain the same information". For a random element, use random.choice, for the first, use next(iter()). Either option satisfies the original requirement - but the first is bound to be faster, so it seems appropriate.
Does anyone have a use-case for retrieving a single arbitrary element of an arbitrary sequence, without caring about any other elements? Is this really such a common operation that we need to consider it part of the interface for all collections? I doubt it.
Agreed. Paul.