By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

Getting the member of a singleton set

P: n/a
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

May 20 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
En Sun, 20 May 2007 17:27:20 -0300, Arnaud Delobelle
<ar*****@googlemail.comescribió:
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?
Yes, something like peek() or any() would be useful. But you're not
restricted by the builtin methods, you could write your own:

def peek(iterable):
return iter(iterable).next()

maybe converting the possible StopIteration into another exception like
EmptyContainer(ValueError).
Note: it is comparatively easier to do this destructively:
myset.pop()
or to bind a name to the member:
element, = myset
If you know that your set contains exactly one element, I like the later
form.
PS: this problem is not restricted to sets but could occur with many
'container' types.
Yes, and I've seen all your expressions, plus some more, like: for x in
container: break
All of them are rather ugly...

--
Gabriel Genellina

May 20 '07 #2

P: n/a
On May 21, 12:52 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
[...]
Yes, something like peek() or any() would be useful. But you're not
restricted by the builtin methods, you could write your own:

def peek(iterable):
return iter(iterable).next()
Yes, but that's not getting rid of the ugliness, just tucking away
from sight :) *I* would know it's still there.
IMHO a 'peek' method in these container types (maybe not all iterables
because in some cases iter(it).next() changes the state of it, eg when
it is an iterator itself).

--
Arnaud
May 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.