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.

Title: Retrieve an arbitrary element from a set without removing it
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.2, Python 2.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: belopolsky, benjamin.peterson, ipatrol, rhettinger, wrichert
Priority: low Keywords: patch

Created on 2009-10-26 21:07 by wrichert, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
setobject_get.patch wrichert, 2009-10-27 09:08 Patches setobject.[ch] and to provide some_set.get()
Messages (10)
msg94508 - (view) Author: Willi Richert (wrichert) Date: 2009-10-26 21:07
Sometimes, a non-removing pop() is needed. In current Python versions,
it can achieved by one of the following ways:

x = some_set.pop()

for x in some_set: 

x = iter(some_set).next()

More native and clean would, however, be 

The attached patch does this for set(). If this is accepted by the
community, frozenset should be extended as well.
msg94511 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-10-26 21:22
Without tests, this patch is unacceptable.
msg94548 - (view) Author: Willi Richert (wrichert) Date: 2009-10-27 09:09
added tests for get() to
msg94599 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2009-10-28 01:56
Any reason you don't want to call set_next from set_get?

I would say

static PyObject *
set_get(PySetObject *so)
	register Py_ssize_t pos = 0;
	register setentry *entry;
        if (set_next(so, &pos, &entry)) {
              return entry->key;
        /* set appropriate error */
        return NULL;

BTW, what your patch is supposed to do on set().get()?
msg94613 - (view) Author: Willi Richert (wrichert) Date: 2009-10-28 08:13
No particular reason, besides that it is ripped off of pop().

Your solution (omitting "register") gives the same performance. Looks
cleaner, of course.

The patch tries to provide a clean way of "for x in some_set: break", as
explained above. See the recent python-dev mailing list musings.
msg94938 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-11-05 18:42
After a long discussion on python-dev, this proposal is rejected in
favor of adding documentation notes on the ways to non-destructively
retrieve an arbitrary item from a set or frozenset.

Here is an except from the end of the thread:

[Steven D'Aprano]
>> Anyway, given the level of opposition to the suggestion, I'm no longer
>> willing to carry the flag for it. If anyone else -- perhaps the OP --
>> feels they want to take it any further, be my guest.

[geremy condra]
> I've said before that I'd like there to be one, standard way of
> doing this. A function call- set.pick() seems reasonably named
> to me- is probably the cleanest way to do that. Absent that,
> an example in the docs that illustrates the preferred idiom
> would be great. 

Summarizing my opposition to a new set method:
1) there already are at least two succinct ways to get the same effect
2) those ways work with any container, not just sets
3) set implementations in other languages show that this isn't needed.
4) there is value to keeping the API compact
5) isn't needed for optimization (selecting the same value in a loop
makes no sense)
6) absence of real-world code examples that would be meaningfully improved

[Terry Reedy]

I would be happy to add an example to the docs so that this thread
can finally end.

[Eric Smith]
Please do!

[Terry Reedy]

Leaving this open until I've done the documentation patch.
msg94939 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2009-11-05 18:57
I don't want to pollute python-dev with more hopeless ideas, but I wonder 
if itertools could grow an efficient C-implemented

def first(collection):
   return next(iter(collection))

On the other hand, it probably belongs to recipes more than stdlib.  This 
is not really an iterator tool after all.
msg106592 - (view) Author: (ipatrol) Date: 2010-05-27 01:43
I still see a use in this. I like to use sets for lists of servers or mirrors. There is no compelling reason *not* to add a get() or pick() method, as described in Sets could be used for many things that lists are currently used for. I request for this to be reopened given the lapse since any action on this.
msg106593 - (view) Author: (ipatrol) Date: 2010-05-27 01:46
I support with a check to see if the length is 0, and rename it pick (based on the generic programming and mathematical literature).
msg106594 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-05-27 02:24
Use set.pop().  

Or if you don't want mutation, then use next(iter(s)) or next(iter(s),default).   This technique also works for any collection including dicts and deques and whatnot.
Date User Action Args
2022-04-11 14:56:54adminsetgithub: 51461
2010-11-21 23:26:24rhettingersetstatus: open -> closed
2010-05-27 02:24:29rhettingersetmessages: + msg106594
2010-05-27 01:46:49ipatrolsetmessages: + msg106593
2010-05-27 01:43:55ipatrolsetnosy: + ipatrol
messages: + msg106592
components: + Interpreter Core, - Library (Lib)
2010-05-27 01:36:13eric.smithlinkissue8830 superseder
2009-11-05 18:57:34belopolskysetmessages: + msg94939
2009-11-05 18:42:05rhettingersetpriority: low
resolution: rejected
messages: + msg94938
2009-10-28 08:13:51wrichertsetmessages: + msg94613
2009-10-28 01:56:04belopolskysetnosy: + belopolsky
messages: + msg94599
2009-10-27 09:09:22wrichertsetmessages: + msg94548
2009-10-27 09:08:03wrichertsetfiles: + setobject_get.patch
2009-10-27 09:07:12wrichertsetfiles: - setobject_get.patch
2009-10-26 21:22:41benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg94511
2009-10-26 21:12:13rhettingersetassignee: rhettinger

nosy: + rhettinger
versions: + Python 2.7, - Python 3.1
2009-10-26 21:07:18wrichertcreate