On Sat, Jul 11, 2020 at 1:33 PM David Mertz <me...@gnosis.cx> wrote: > On Sat, Jul 11, 2020 at 3:45 PM Christopher Barker <python...@gmail.com> > wrote: > >> random.choice(the_dict.keys()) >> >> is a little easier than: >> >> random.choice(list(the_dict.keys()) >> > > Ummm... don't you mean: > > random.choice(list(the_dict)) >
well, sure, though I have to say that I think that that's an unfortunate confusing thing about python dicts. IN fact, I doubt there are many uses at all for dict.keys() -- most uses can jsut use the dict. I certainly see newbies write: if this in dict.keys(): pretty often. maybe adding indexing to the dict views will give the dict_keys object more reason to exist :-) -CHB > If it's keys you care about I've saved you one character over your > proposed style, while also reading better to me. It's only for .items() > where it doesn't work. And honestly, just looking up the value from the > random key is not hard. > > In any case, if "reservoir sampling" is the goal here, we should just add > a function `random.reservoir_sample()` to accommodate using iterators > rather than sequences (https://en.wikipedia.org/wiki/Reservoir_sampling) > > > -- > The dead increasingly dominate and strangle both the living and the > not-yet born. Vampiric capital and undead corporate persons abuse > the lives and control the thoughts of homo faber. Ideas, once born, > become abortifacients against new conceptions. > -- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ 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/3RPW7EPXHICBPM4RXENZNZAXRGJJDKVV/ Code of Conduct: http://python.org/psf/codeofconduct/