As was discussed the last time, I don’t know that selecting a random item from a dict has a lot of use cases — I’m pretty sure the only time I’ve needed to do it was for Dave Thomas’s “trigrams” coding kata. Though I’m not sure I’ve used random.choice fir anything mor “real” either. 

But it is a more compelling reason to add a feature to dict than getting the first or last item :-)

-CHB

On Sat, Oct 9, 2021 at 7:55 PM David Mertz, Ph.D. <david.mertz@gmail.com> wrote:
On Sat, Oct 9, 2021, 10:44 PM Steven D'Aprano 
Apart from a programming exercise and teaching moment, why would you want to get a random key and value from a dict? In 25-ish years of using Python, I think that the number of times I've needed to do that is zero. A random item from a list, dozens of times. But from a dict, never.
(I'm not even convinced that dict.popitem is useful either. But maybe that's just me, and others use it ten times a day.)

I've used dict.popitem() from time to time. Usually for the purpose of consuming items in a loop. But if I want to, I can put items back when I want to.

I don't see a need for anything past that. On rare occasion, this is fine:

somekey, someval = mydict.popitem()
mydict[somekey] = someval
do_stuff(somekey, someval)
_______________________________________________
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/5ILH4YZGHNIDE3NJZTQNEDKHINIWKF2F/
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