[Python-Dev] Dict.popitem(key=None)

Raymond Hettinger python@rcn.com
Wed, 3 Apr 2002 18:26:17 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_0029_01C1DB3D.0B3698E0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Dan Parisien posted a small feature request about three months ago:  =
http://sourceforge.net/tracker/index.php?func=3Ddetail&aid=3D495086&group=
_id=3D5470&atid=3D355470

The current behavior of popitem is to return and remove an arbitrary =
key/value pair from a dictionary.  With the optional argument, Dan =
proposes that a specific key/value pair be returned and removed.  =
Without the optional argument, the behavior would be unchanged.

I think this is a great idea.  Like .get() and .setdefault(), this =
provides a fast, explicit alternative to a commonly used series of =
dictionary operations.  The speed improvement comes from doing a single =
lookup for retrieving the item and then deleting it.  Like the optional =
argument in list.pop(i), the extra ability is added without cluttering =
the default case.

If we can agree that this should be done and accepted, I'll volunteer to =
write the patch.


Raymond Hettinger



Before
--------
value =3D adict[key]
del key
return (key, value)

After
------
return adict.popitem(key)


------=_NextPart_000_0029_01C1DB3D.0B3698E0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4207.2601" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>
<DIV><FONT face=3DArial size=3D2>Dan Parisien posted a small feature =
request about=20
three months ago:&nbsp; <A=20
href=3D"http://sourceforge.net/tracker/index.php?func=3Ddetail&amp;aid=3D=
495086&amp;group_id=3D5470&amp;atid=3D355470">http://sourceforge.net/trac=
ker/index.php?func=3Ddetail&amp;aid=3D495086&amp;group_id=3D5470&amp;atid=
=3D355470</A></FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>The current behavior of popitem is to =
return and=20
remove an arbitrary key/value pair&nbsp;from a dictionary.&nbsp; With =
the=20
optional argument, Dan proposes that a specific key/value pair be =
returned and=20
removed.&nbsp; Without the optional argument, the behavior would be=20
unchanged.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I think this is a great idea.&nbsp; =
Like .get() and=20
.setdefault(), this provides a fast, explicit alternative to a commonly =
used=20
series of dictionary operations.&nbsp; The speed improvement comes from =
doing a=20
single lookup for retrieving the item and then deleting it.&nbsp; =
</FONT><FONT=20
face=3DArial size=3D2>Like the optional argument in list.pop(i), the =
extra ability=20
is added without cluttering the default case.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>If we can agree that this should be =
done and=20
accepted, I'll volunteer to write the patch.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Raymond Hettinger</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>Before</DIV>
<DIV>--------</DIV>
<DIV>value&nbsp;=3D adict[key]</DIV>
<DIV>del key</DIV>
<DIV>return&nbsp;(key, value)</DIV>
<DIV>&nbsp;</DIV>
<DIV>After</DIV>
<DIV>------</DIV>
<DIV>return adict.popitem(key)</DIV>
<DIV>&nbsp;</DIV></FONT></DIV></BODY></HTML>

------=_NextPart_000_0029_01C1DB3D.0B3698E0--