Please do go back and read that previous thread, there was a LOT of discussion and much detail. "I want to use first on dicts" is not really a use-case. quite true -- what IS the use case(s) here? But honestly, I'm even more confused by the desire for the last item -- what's the use case for that? I can see a use case for "arbitrary item", for which the first would work. There is also the use for a random item, and random.choice does not work with Mappings, but would work if we made dict.items() indexable. But there doesn't seem to be a lot of clamoring for that. And if there were, maybe adding it as a feature to random.choice would be the way to go. NOTE: A key issue with why indexable dict,items() could be an attractive nuisance if one were to index into it many times: IF you want one first, last, or random item, then having it built in would be the fastest way to go. but if you wanted multiple random or other indexed items, then making a list out of it ( O(n) ), and then indexing ( O(1) ) many times would be more efficient. So saying "the way to index the contents of a dict is to index: list(dict.items()) is good advice. As for a first in itertools, this is running into the way Python provides (almost) the same interface to iterables as iterators -- first() makes some sense for iterables, but none for iterators, so confusion is likely. We don't bloat classes and the builtins with named
trivial one-liners unless they are frequently useful, fundamental and really compelling:
indeed. float.add_one() # return float + 1.0
float.double() # return float*2.0 list.length_equals_one() # return len(list) == 1
Come on, those examples are pretty disingenuous. They are analogous to adding first() to the Sequence ABC, when we have [0] -- which the OP has clearly said is unnecessary. As for the argument that the idiom `next(iter(obj))` is "not intuitive",
true. Neither is `seq[0]`, or with statements, or range, or classes, or importing, or async, or comprehensions, or regular expressions, or pretty much everything else in Python.
"intuitive" is impossible to pin down -- but using next(iter(obj)) is a advanced topic (or intermediate anyway) -- you can get a LOT done in Python without ever using next() or iter() -- they are far more often called implicitly (via for loops, making lists, etc). I've said it before, "getting a random (or even arbitrary) item from a dict" is a rarity in Python, there is not an easy and obvious way to it (without knowing the iteration protocol). Whether that's something people need to do often enough to justify a new function is another story, but it is NOT as trivial as making a function that adds one to something. -CHB
And yet somehow we cope.
It is okay to learn how do something.
-- Steve _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/SAZLCI... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython