[Python-bugs-list] [ python-Feature Requests-495086 ] dict.popitem(key=None)

noreply@sourceforge.net noreply@sourceforge.net
Sun, 28 Apr 2002 09:09:33 -0700


Feature Requests item #495086, was opened at 2001-12-19 17:26
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=495086&group_id=5470

Category: Python Interpreter Core
Group: None
>Status: Closed
>Resolution: Accepted
Priority: 5
Submitted By: Dan Parisien (mathematician)
Assigned to: Nobody/Anonymous (nobody)
Summary: dict.popitem(key=None)

Initial Comment:
Would it be possible to add an extra argument to the 
popitem method of DictionaryType so one can both 
retrieve a dict item and delete it at the same time? 
It would be so handy. Without the optional argument, 
it would work the same way dict.popitem works now

example::

>>> d = dict([(x,x) for x in range(10)])
>>> d
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 
8, 9: 9}
>>> d.popitem() # retrieves "random" key->val pair
(0, 0)
>>> d.popitem(4) # val=d[4]; del d[4]; return val
4
>>> d.popitem(6) # val=d[6]; del d[6]; return val 6
>>> d # missing keys [0, 4, 6]
{1: 1, 2: 2, 3: 3, 5: 5, 7: 7, 8: 8, 9: 9}





----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-04-28 16:43

Message:
Logged In: YES 
user_id=80475

The patch for dict.popitem(k) was rejected on the grounds 
that 1) the optional argument slowed down popitem and 2) 
returning (k,v) didn't make sense when k was already 
known.  A revised patch for for dict.pop(k) --> v was 
accepted, loaded to CVS, and closed.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-04-05 21:44

Message:
Logged In: YES 
user_id=80475

A patch implementing this request is at:
http://sourceforge.net/tracker/index.php?
func=detail&aid=539949&group_id=5470&atid=305470

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-02-20 09:22

Message:
Logged In: YES 
user_id=80475

Great idea!

The rationale is just like that for .setdefault() in 
providing a fast, simple, single method, single look-up 
replacement for a commonly used sequence of dictionary 
operations.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-02-17 01:17

Message:
Logged In: YES 
user_id=21627

Also requested as

http://sourceforge.net/tracker/index.php?func=detail&aid=504880&group_id=5470&atid=355470

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=495086&group_id=5470