[Patches] [ python-Patches-633374 ] nondestructive dict.popitem and Set.pop

noreply@sourceforge.net noreply@sourceforge.net
Thu, 07 Nov 2002 12:42:23 -0800

Patches item #633374, was opened at 2002-11-04 12:15
You can respond by visiting: 

Category: Library (Lib)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: John Williams (johnw42)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: nondestructive dict.popitem and Set.pop

Initial Comment:
This patch (relative to the latest Python CVS tree)
adds a "pickitem" method to the builtin dict class and
a "pick" method to the BaseSet class.  These methods
are analogs of "dict.popitem" and "Set.pop", but they
don't remove the item they return from the dict/set.

This patch *does not* update the documentation.

This is my system:
Linux 2.4.2-2 #1 i686 unknown


>Comment By: Tim Peters (tim_one)
Date: 2002-11-07 15:42

Logged In: YES 

Assigned to Guido for Pronouncement.

I've rarely had a need to select "an arbitrary" dict entry 
*without* also removing it from a dict.  In the few cases that's 
come up, I've done

k, v = dict.popitem()
dict[k] = v

happily.  We seem to be missing a compelling use case here.


Comment By: John Williams (johnw42)
Date: 2002-11-05 10:20

Logged In: YES 

There's no technical reason why this patch is necessary, but
I think having it would make it easier to write clean and
readable code. It seems very unintuitive to me that the
nondescructive analog of "popitem()" would be
"iteritems().next()". Even though I'm pretty familiar with
iterators, this solution did not occur to me immediately,
and I suspect a large portion of Python's users don't even
know about using iterators this way. The "items()[0]"
solution is even worse, IMHO, since it involves generating a
whole list just to get a single item. I was also trying to
preserve the similarity between dicts and sets, and both the
list solution and the iterator solution look pretty
different when used on sets.

Also, using an iterator fails with a StopIteration exception
when the dict/set is empty, but the methods in the patch
raise KeyError with a helpful error string explaining the
problem, just like pop and popitem.

I wouldn't venture to guess how often others would use these
methods; I just know I would have found them helpful recently.


Comment By: Raymond Hettinger (rhettinger)
Date: 2002-11-05 01:31

Logged In: YES 

dict.popitem() was added because the it could retrieve and 
delete a key/value pair without hashing -- there were no 
existing methods which could achieve the same result.

In contract, the dict.pickitem() patch doesn't appear to 
offer a differential advantage over dict.iteritems().next() for 
retrieving an arbitrary (hash order) key/value pair.

Also, since successive calls to pickitem() retrieve the same 
pair, it doesn't appear to be useful in a loop or warrant a C 
speed optimization


Comment By: Neal Norwitz (nnorwitz)
Date: 2002-11-04 18:54

Logged In: YES 

Fixed the Summary, lest someone think you were making a
personal problem public. :-)

Seriously though...I haven't looked at the patch, but could
you explain the rationale/benefit?  Is this likely to be
useful to many people or is it fairly limited?  Couldn't you
do dict.items()[0] if you wanted a random value?


You can respond by visiting: