text: Created on 2009-10-26 21:07 by wrichert, last changed 2010-11-21 23:26 by rhettinger. This issue is now closed.
  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, however, be
  some_set.get()
  The attached patch does this for set(). If this is accepted by the
  community, frozenset should be extended as well.
  Without tests, this patch is unacceptable.
  added tests for get() to test_set.py
  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(entry->key);
  return entry->key;
  }
  /* set appropriate error */
  return NULL;
  }
  BTW, what your patch is supposed to do on set().get()?
  }
  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..
  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.
  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.
  I support with a check to see if the length is 0, and rename it pick (based on the generic programming and mathematical literature).
  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.