[Python-ideas] random.choice on non-sequence

Rob Cliffe rob.cliffe at btinternet.com
Wed Apr 13 05:47:53 EDT 2016

On 13/04/2016 09:32, Paul Moore wrote:
> On 13 April 2016 at 06:29, Joao S. O. Bueno<jsbueno at python.org.br>  wrote:
>> Maybe, instead of all of this, since as put by others, hardly there would be a
>> "one size fits all" - the nice thing to have would be a "choice" or
>> "randompop"  method for sets.
> Given that your choice method for sets would be little more than
>      value = random.choice(tuple(s))
> it seems like it's probably overkill to build it into Python -
> particularly as doing so restricts the implementation to Python 3.6+
> whereas writing it out by hand works for any version. It also makes
> the complexity of the operation explicit, which is a good thing.
> Paul
> _______________________________________________
Isn't there an inconsistency that random.sample caters to a set by 
converting it to a tuple, but random.choice doesn't?
Surely random.sample(someSet, k) will always be slower than a 
"tuple-ising" random.choice(someSet) implementation?
And it wouldn't prevent you from converting your set (or other iterable) 
to a sequence explicitly.

More information about the Python-ideas mailing list