RE: random choice from dictionary

2018-12-23 Thread Avi Gross
and they are not likely to be from a python dictionary. -Original Message- From: Python-list On Behalf Of MRAB Sent: Sunday, December 23, 2018 4:21 PM To: python-list@python.org Subject: Re: random choice from dictionary On 2018-12-23 19:52, Avi Gross wrote: > There are quite a few places the new pytho

Re: random choice from dictionary

2018-12-23 Thread sarah123ed
Maybe something like this: import random k = list(data.keys()) random.shuffle(k) result = data[k[0]] -- https://mail.python.org/mailman/listinfo/python-list

Re: random choice from dictionary

2018-12-23 Thread sarah123ed
Maybe something like this? import random k = list(data.keys()) random.shuffle(k) selected = f[k[0]] -- https://mail.python.org/mailman/listinfo/python-list

Re: random choice from dictionary

2018-12-23 Thread Ben Bacarisse
"Avi Gross" writes: > The following is a function that iterates over the dictionary one key at a > time and when it reaches a random one, it returns the key without further > evaluation. On the average, it takes N/2 iterations for N keys. Asking to > make a list(data) may be efficient in terms

Re: random choice from dictionary

2018-12-23 Thread MRAB
On 2018-12-23 19:52, Avi Gross wrote: There are quite a few places the new pythonic way of doing things requires extra steps to get an iterator to expand into a list so Abdul-Rahmann probably is right that there is no easy way to get a random key from a standard dictionary. Other than the

RE: random choice from dictionary

2018-12-23 Thread Avi Gross
There are quite a few places the new pythonic way of doing things requires extra steps to get an iterator to expand into a list so Abdul-Rahmann probably is right that there is no easy way to get a random key from a standard dictionary. Other than the expected answers to make a customized

Re: random choice from dictionary

2018-12-23 Thread Abdur-Rahmaan Janhangeer
random.choice(list(data)) is great. fine with arbitrary selection. else, given data.keys(), how do i get the first key? yours -- Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius

Re: random choice from dictionary

2018-12-23 Thread Chris Angelico
On Sun, Dec 23, 2018 at 7:34 PM Abdur-Rahmaan Janhangeer wrote: > > greetings, > > just a check, is this: > > random.choice(list(data.keys())) > > the only way to get a random key from a dictionary? if not any plan to a > more convenient naming? Does it have to be truly random, or are you okay