[Python-Dev] PEP-0218

M.-A. Lemburg mal@lemburg.com
Thu, 30 Nov 2000 09:30:22 +0100

Moshe Zadka wrote:
> > BTW, one lesson to take from SETL:  a vital set operation in practice is a
> > mutating "pick a 'random' element E from the set, remove it from the set,
> > and return it".  An enormous number of set algorithms are of the form
> >
> >     while not S.empty():
> >         pick some element from S
> >         deal with it, possibly mutating S
> >
> > This operation can't be done efficiently in Python code if the set is
> > represented by a dict (the best you can do is materialize the full list of
> > keys first, and pick one of those).  That means my Set class often takes
> > quadratic time for what *should* be linear-time algorithms.
> Hmmm...actually, I've been wanting a method .key() for dictionaries
> a long time. So if we give dictionaries this one small method, then
> we *can* do this in Python.

Shouldn't be hard to do... the C API for this is already in place:
PyDict_Next(). I'd prefer a method .getitem(), though, which then
returns a tuple (key, value).

Marc-Andre Lemburg
Company:                                        http://www.egenix.com/
Consulting:                                    http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/