Suppose the following:
>>> s = set([1, 2, 3])
How do I get a value (any value) out of
s without doing
s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to another host.
Quick and dirty:
>>> elem = s.pop() >>> s.add(elem)
But do you know of a better way? Ideally in constant time.
Two options that don't require copying the whole set:
for e in s: break # e is now an element from s
e = next(iter(s))
But in general, sets don't support indexing or slicing.
Least code would be:
>>> s = set([1, 2, 3]) >>> list(s) 1
Obviously this would create a new list which contains each member of the set, so not great if your set is very large.