How to get an item from a simple set?
Skip Montanaro
skip at pobox.com
Wed Nov 24 10:46:50 EST 2004
Pete> I have a set that contains one item. What is the best way of
Pete> getting at that item? Using pop() empties the set.
Do you want to enumerate all the items in the set? If so:
for elt in s:
print elt
If you just want to grab one arbitrary (though not random) item from the
set, try:
elt = iter(s).next()
Note that repeating this operation will always return the same item:
>>> s
set(['jkl', 'foo', 'abc', 'def', 'ghi'])
>>> iter(s).next()
'jkl'
>>> iter(s).next()
'jkl'
>>> iter(s).next()
'jkl'
>>> iter(s).next()
'jkl'
Skip
More information about the Python-list
mailing list