
On Fri, Jul 10, 2020 at 7:20 AM Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, Jul 09, 2020 at 10:25:46PM +1000, Chris Angelico wrote:
Getting a random element from a dict (not an arbitrary one but a random one) definitely does have a use-case. I've wanted it at times. Usually if I'm doing this in Python, I just pay the price and build the list, but as proof that it's a logical and useful operation, here's Pike's general random function:
http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/random.ht...
If given a number, it picks a random number. If given an array, it picks a random element. And if given a mapping (dictionary), it returns (key,value), without first converting to a flat list.
That's an existance proof that somebody else implemented the function, not a use-case. Getting a random key/item pair directly from a dict is a mechanism, not an explanation of why you need a random key-item pair.
And immediately above that part, I said that I had made use of this, and had used it in Python by listifying the dict first. Okay, so I didn't actually dig up the code where I'd done this, but that's a use case. I have *actually done this*. In both languages. ChrisA