Alex Waygood writes:
the suggestion was to add two functions to itertools (first() and last(), which would work with any iterable,
OK, that wasn't obvious to me either, but good enough. I see the analogy to str's startswith and endswith, but I'm still -0 on these. The names suggest indexing by position, ie, a non- destructive operation. For sequences, they are non-destructive, but they're just alternative spellings of [0] and [-1], with the added bonus (?!) of extra overhead. But with iterators, they will be destructive, and first() is just a synonym for next() (with a tiny bit of extra overhead). IMO, that's icky, YMMV. I'm not terribly sympathetic to the argument that "Python beginners use inefficient alternatives". A Python beginner who needs this to be both efficient and polymorphic over sequences and iterators is already 10 feet down in the deep end, head-first. I'm sympathetic to the *beginner* -- I've found myself in that situation a half-dozen times with a half-dozen different languages. Thing is, one more idiom isn't going to kill the camel. In learning all the other stuff they'll need to know about iterables, the odds are good they'll pick this up en passant. I've had that experience, too, about 5 of 6 times, and in the last case Sempai noticed me and had pity. Steve