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 :-)


On Sat, Oct 9, 2021 at 7:55 PM David Mertz, Ph.D. <> 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 --
To unsubscribe send an email to
Message archived at
Code of Conduct:
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython