Feature Requests item #1212091, was opened at 2005-05-31 10:54 Message generated for change (Comment added) made by rhettinger You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1212091&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Library Group: None Status: Open Resolution: None Priority: 5 Submitted By: Mike Coleman (mkc) Assigned to: Nobody/Anonymous (nobody) Summary: sets needs an 'arbitrary element' method Initial Comment: It would be nice to have an "arbitrary element" method that would return some arbitrary element of a non-empty set (throwing an exception if the set is empty). Something like this >>> s = sets.Set([1,2,3]) >>> print s.element() 2 AFAIK, the current alternative would be to do something like this >>> print list(s)[0] which is somewhat expensive and unreadable. It'd be fine if the operator returned the same or a different element each time. Perhaps it'd be useful if it returned the same element each time for frozen sets. ---------------------------------------------------------------------- >Comment By: Raymond Hettinger (rhettinger) Date: 2005-05-31 11:54 Message: Logged In: YES user_id=80475 I had looked at putting in a choose() method but there were a shortage of use cases that were not better served by pop() or by iteration. Will leave this open as the request may yet prove its worth. To do so, I would need to see examples of practical code where choose() is a better choice than existing alternatives. For the record, here were some of the lines of thinking about choose(). * Given that any element could be returned, it was logical to return the first encountered. That led to the odd situation where the method would return the same value on every call (unless followed by a set mutation) making the method useless in a loop. * If needed, an efficient alternative is available: iter(s).next(). And here is a more readable, encapsulated version that a programmer could dash off in seconds: def choose(s, default=None): for elem in s: return elem return default * I had looked at a few textbook algorithms that were expressed in terms of choose(). Without exception, their Python code was better expressed using pop(), In a couple of cases, the pop() needed to be followed by an add() if the item was to be left in the set. Those two cases all had other set mutations occuring on each iteration (otherwise, the pop/add pair would always get the same element). ---------------------------------------------------------------------- Comment By: Reinhold Birkenfeld (birkenfeld) Date: 2005-05-31 10:59 Message: Logged In: YES user_id=1188172 For mutable sets, I think that pop() is better suited. Do you have a use case of your proposal with frozensets? ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1212091&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com