
On 09.07.20 14:25, Chris Angelico wrote:
On Thu, Jul 9, 2020 at 9:16 PM Steven D'Aprano <steve@pearwood.info> wrote:
Unless I have missed any others, we've only seen three use-cases:
(a) The original post wanted a full sequence API for dictionaries, with the ability to insert keys at a specific index, not just get the N-th key. Not going to happen.
(b) You've suggested "get any item", but that's probably better written as `next(mydict.items)`.
(c) And `random.choice(mydict.items())` which seems to be lacking any *concrete* use-case -- under what circumstances would we want this and care about it's performance *enough* to add this to builtin dict views?
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.
This doesn't seem to be an argument for modifying dict views since the actual "issue" lies with `random.choice` and the way it can (or cannot) interact with the containers its given. Even when implemented on dict views, this still won't for work for sets for example. And it's definitely reasonable to ask "pick a random element from this set" (in terms or real world language). But from the perspective of the set API there is no way to pick specific elements and hence it must either rely on conversion to a sequence or rely on implementation details of the set data structure. So if the use case is to get a random element from a dict, the discussion should rather be centered around `random.choice` and also respect other non-sequence containers.