Before 3.6 I would simply use set.pop(). Now, sets are ordered and pop always removes from the start.
What do you think is the most pythonic way?
I'm not sure how I feel about set.remove(random.sample(set, 1)[0]).
Before 3.6 I would simply use set.pop(). Now, sets are ordered and pop always removes from the start.
What do you think is the most pythonic way?
I'm not sure how I feel about set.remove(random.sample(set, 1)[0]).
The set .pop() method does not accept a parameter, it removes an arbitrary (but not random) set value. https://docs.python.org/3.6/library/stdtypes.html#set.pop
Take a set sample with one element random.sample(s, 1) and use set.remove().
>>> s = set([1,2,3,4,5])
>>> el = random.sample(s, 1)[0]
>>> s.remove(el)
>>> print(s)
{1, 3, 4, 5}
The random.choice does not work because sets are not indexed.
>>> random.choice(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/random.py", line 256, in choice
return seq[i]