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:
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
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  -- 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`, 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.
And yet somehow we cope.
It is okay to learn how do something.
Python-ideas mailing list -- firstname.lastname@example.org
To unsubscribe send an email to email@example.com
Message archived at https://firstname.lastname@example.org/message/SAZLCIVFOFNZE3Q2K3TOYM6L2WU4DSTJ/
Code of Conduct: http://python.org/psf/codeofconduct/
Christopher Barker, PhD (Chris)
Python Language Consulting
- Scientific Software Development
- Desktop GUI and Web Development
- wxPython, numpy, scipy, Cython