[Python-Dev] Retrieve an arbitrary element from a set withoutremoving it

Steven D'Aprano steve at pearwood.info
Fri Oct 30 03:58:16 CET 2009

On Wed, 28 Oct 2009 04:47:59 am Raymond Hettinger wrote:

> A dict.get() can be meaningfully used in a loop (because the key can
> vary). A set.get() returns the same value over and over again
> (because there is no key).

I don't believe anyone has requested those semantics. The suggested 
semantics for set.get() with no arguments, as I understand them, are:

(1) it will only fail if the set is empty;

(2) it should be efficient;

(3) if you call it repeatedly on a set without modifying the set, you 
will cycle through each element in turn in some unspecified arbitrary 

To clarify point 3, given:

x = set.get()
y = set.get()

then x and y will only be the same element if set has length one. 
However, given:

x = set.get()
y = set.get()

there are no guarantees about x and y being different.

I believe that the patch supplied by Willi Richart implemented these 


Steven D'Aprano

More information about the Python-Dev mailing list