
Nick Coghlan ha scritto:
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.
It's not particularly wasteful for the built-in data structures, though. It seems to me that the cases where the performance of next(iter(obj)) would be an actual issue are quite rare.
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.
Why? next(iter(obj)) means, pretty explicitly to me, "iterate on obj and give me one element". matteo