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

Guido van Rossum guido at python.org
Wed Apr 13 12:16:55 EDT 2016

On Wed, Apr 13, 2016 at 8:31 AM, Chris Barker - NOAA Federal
<chris.barker at noaa.gov> wrote:
>> There's an upper limit on how dense a CPython dict or set can become
>> (the load factor doesn't exceed 2/3), but no lower limit.  For
>> example, it's easy to end up with a dict holding a single entry hiding
>> among millions of empty slots (dicts are never resized on key
>> deletion, only on key insertion).
> Easy, yes. Common? I wonder. If it were common then wouldn't there be
> good reason to resize the hash table when that occurred? Aside from
> being able to select random items, of course...

So it's becoming clear that if we wanted to do this right for sets
(and dicts) the choice() implementation should be part of the set/dict
implementation. It can then take care of compacting the set if it's
density is too low.

But honestly I don't see the use case come up enough to warrant all that effort.

--Guido van Rossum (python.org/~guido)

More information about the Python-ideas mailing list