Getting the member of a singleton set
Arnaud Delobelle
arnodel at googlemail.com
Sun May 20 16:27:20 EDT 2007
Hi all,
I often find myself needing to get (non-destructively) the value of
the member of a singleton set. Is there a good way to do this (as an
expression?) None of the ones I can think of satisfy me, eg:
* list(myset)[0]
* iter(myset).next()
* set(myset).pop()
What I would like is something like a 'peek()' function or method
(would return the same as pop() but wouldn't pop anything). I would
like to know of a better idiom if it exists. If not, isn't there a
need for one?
Note: it is comparatively easier to do this destructively:
myset.pop()
or to bind a name to the member:
element, = myset
PS: this problem is not restricted to sets but could occur with many
'container' types.
--
Arnaud
More information about the Python-list
mailing list