+1. These would be handy for any iterable. It'll works on dict keys and values; bonus.
On Wed, 2021-10-06 at 15:42 +0100, Alex Waygood wrote: > > Whether they are added to dict or itertools, there are still nine > > of > > them > > No, the suggestion was to add two functions to itertools (first() and > last(), which would work with any iterable, not just dicts), rather > than adding nine methods to the dict interface. This was precisely > why I was saying that I liked the itertools solution more. > > > On 6 Oct 2021, at 15:01, Steven D'Aprano <st...@pearwood.info> > > wrote: > > > > On Wed, Oct 06, 2021 at 11:11:09AM +0100, Alex Waygood wrote: > > > > The temptation to insist "see, YAGNI!" at this point I shall > > > > resist. > > > > > > *You* might not need it, but I've seen it come up a lot on Stack > > > Overflow, and all too often people end up going for the much less > > > efficient solution. I personally have also written code with > > > practical > > > applications using `next(iter(mydict))`. > > > > Under what circumstances do you care what the first key in a dict > > is, > > without going on to care about the second, third, fourth etc? > > > > They are surely extremely niche, or artificial, or both, e.g. the > > Stackoverflow problem you link to: "find the first non-repeating > > character in a string -- using only one loop". Why the *first* > > rather > > than any, or all? > > > > In any case, the presence of one or two uses for a piece of > > functionality doesn't mandate that we make this a builtin. Your > > solution > > with next() is perfectly adequate. > > > > The other suggested methods are even more obscure. Why have a > > method > > for returning the first value, without knowing the key? > > > > "I don't know what the first key is, and I don't care, but I know > > that > > whatever it is, it maps to the value 17." > > > > Now what are you going to do with that knowledge? This seems like a > > method in desperate need of a use-case. > > > > > > [...] > > > I agree that it's a lot of methods to add. That's precisely why I > > > prefer Inada Naoki's suggestion of additions to itertools > > > > Whether they are added to dict or itertools, there are still nine > > of > > them, and they are pretty much near clones of each other: > > > > # first_ and last_ whatsits > > next([iter|reversed](obj.[keys|values|items]())) > > > > if you will excuse the misuse of hybrid Python/BNF syntax :-) > > > > > > > > -- > > Steve > > _______________________________________________ > > Python-ideas mailing list -- python-ideas@python.org > > To unsubscribe send an email to python-ideas-le...@python.org > > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > > Message archived at https://mail.python.org/archives/list/python- > > id...@python.org/message/T3TOFAFBPGY44LOVKSMVZJGBNQ7MUNEL/ > > Code of Conduct: http://python.org/psf/codeofconduct/ > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@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/OCVRRJPZQCQ6UKVKR4GQRJFWAUJNDFK6/ > Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/5EJWQFXWOERWNRF4QEIESGY3DPHZZFOA/ Code of Conduct: http://python.org/psf/codeofconduct/