random.choice from set? python
Note (Oct. 2020): as of v3.9, Python has officially deprecated random.sample()
working on sets, with the official guidance being to explicitly convert the set to a list or tuple before passing it in, though this doesn't solve the efficiency problems.
>>> random.sample(set('abcdefghijklmnopqrstuvwxyz'), 1)
['f']
Documentation: https://docs.python.org/3/library/random.html#random.sample
Note that choosing random elements from a set is extremely inefficient no matter how you do it - it takes time proportional to the size of the set, or worse if the set's underlying hash table is sparse due to removed elements.
Instead, you should probably use a different data structure that supports this operation efficiently.
You should use random.choice(tuple(myset))
, because it's faster and arguably cleaner looking than random.sample
. I wrote the following to test:
import random
import timeit
bigset = set(random.uniform(0,10000) for x in range(10000))
def choose():
random.choice(tuple(bigset))
def sample():
random.sample(bigset,1)[0]
print("random.choice:", timeit.timeit(choose, setup="global bigset", number=10000)) # 1.1082136780023575
print("random.sample:", timeit.timeit(sample, setup="global bigset", number=10000)) # 1.1889629259821959
From the numbers it seems that random.sample
takes 7% longer.