dict.popitem(key) ???

Raymond Hettinger vze4rx4y at verizon.net
Sun Mar 16 07:08:53 CET 2003

"Justin Shaw"
> Why doesn't popitem take a key argument that defaults to None?  Or how
> should I pop a particular item out of a dict?

In Py2.3, dict.pop can take an optional default argument:

   avalue =  mydict(possiblekey, default)

It can also just search for a specified key and return an
exception if not found:

  avalue = mydict(possiblekey)

The semantics of popitem() were not altered because:
* the zero argument form is faster than a variable argument form
* why return a (key,value) tuple when the key is already known
* the dict.pop() form parallels d.get() and getattr().

Raymond Hettinger

More information about the Python-list mailing list