[issue7212] Retrieve an arbitrary element from a set without removing it

2010-11-21 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- status: open -> closed ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: http://

[issue7212] Retrieve an arbitrary element from a set without removing it

2010-05-26 Thread Raymond Hettinger
Raymond Hettinger added the comment: 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. -- ___ Python tracker

[issue7212] Retrieve an arbitrary element from a set without removing it

2010-05-26 Thread ipatrol
ipatrol added the comment: I support http://bugs.python.org/msg94599 with a check to see if the length is 0, and rename it pick (based on the generic programming and mathematical literature). -- ___ Python tracker

[issue7212] Retrieve an arbitrary element from a set without removing it

2010-05-26 Thread ipatrol
ipatrol added the comment: 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 http://en.wikipedia.org/wiki/Set_%28computer_science%29. Sets could be used for many things that lists

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-11-05 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: 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. Thi

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-11-05 Thread Raymond Hettinger
Raymond Hettinger added the comment: 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] >> A

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-28 Thread Willi Richert
Willi Richert added the comment: 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 pyth

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Alexander Belopolsky
Alexander Belopolsky added the comment: 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)) { Py_INCREF(ent

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Willi Richert
Willi Richert added the comment: added tests for get() to test_set.py -- ___ Python tracker ___ ___ Python-bugs-list mailing list Unsu

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Willi Richert
Changes by Willi Richert : Added file: http://bugs.python.org/file15211/setobject_get.patch ___ Python tracker ___ ___ Python-bugs-list mailing

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-27 Thread Willi Richert
Changes by Willi Richert : Removed file: http://bugs.python.org/file15207/setobject_get.patch ___ Python tracker ___ ___ Python-bugs-list maili

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-26 Thread Benjamin Peterson
Benjamin Peterson added the comment: Without tests, this patch is unacceptable. -- nosy: +benjamin.peterson ___ Python tracker ___ ___

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-26 Thread Raymond Hettinger
Changes by Raymond Hettinger : -- assignee: -> rhettinger nosy: +rhettinger versions: +Python 2.7 -Python 3.1 ___ Python tracker ___ _

[issue7212] Retrieve an arbitrary element from a set without removing it

2009-10-26 Thread Willi Richert
New submission from Willi Richert : Sometimes, a non-removing pop() is needed. In current Python versions, it can achieved by one of the following ways: 1. x = some_set.pop() some_set.add(x) 2. for x in some_set: break 3. x = iter(some_set).next() More native and clean would, howev