This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients docs@python, terry.reedy
Date 2012-05-16.22:41:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1337208090.37.0.632127020372.issue14836@psf.upfronthosting.co.za>
In-reply-to
Content
There have been several requests for a set.get() (no args) or set.pick() method to get an item without deleting it as .pop() does. Probably the best answer is to use the simple generic composition next(iter(s)). The counter response is that it is hardly obvious and needs to be documented.

Suggesion: after the current

"pop() 
Remove and return an arbitrary element from the set. Raises KeyError if the set is empty."

add "Use next(iter(s)) to return an arbitrary element without removing it."

Then change the following to match.

"popitem() 
Remove and return an arbitrary (key, value) pair from the dictionary.

popitem() is useful to destructively iterate over a dictionary, as often used in set algorithms. If the dictionary is empty, calling popitem() raises a KeyError."

to

"popitem() 
Remove and return an arbitrary (key, value) pair from the dictionary. Raises KeyError if the dict is empty. Use next(iter(d)) to return an arbitrary pair without removing it."

The old comment about destructively iterating over a dict as a set belongs more with set.pop if it is not removed.

The idiom works with all iterators, but there is no other place I can think of to put it, and it is not needed for sequences.
History
Date User Action Args
2012-05-16 22:41:30terry.reedysetrecipients: + terry.reedy, docs@python
2012-05-16 22:41:30terry.reedysetmessageid: <1337208090.37.0.632127020372.issue14836@psf.upfronthosting.co.za>
2012-05-16 22:41:29terry.reedylinkissue14836 messages
2012-05-16 22:41:29terry.reedycreate