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-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/5ILH4YZGHNIDE3NJZTQNEDKHINIWKF2F/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to