How to get an item from a simple set?

Pete Forman pete.forman at westerngeco.com
Wed Nov 24 11:16:42 EST 2004


Skip Montanaro <skip at pobox.com> writes:

 >     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.  
 > 
 > If you just want to grab one arbitrary (though not random) item from the
 > set, try:
 > 
 >     elt = iter(s).next()

I actually wanted to append the single item to a string,  Steven's
solutions work for assignment.

So this looks like my best bet.  I'll probably use join instead of +=
in my code.

>>> line = 'bar '
>>> line += iter(s).next()
>>> line
'bar foo'

-- 
Pete Forman                -./\.-  Disclaimer: This post is originated
WesternGeco                  -./\.-   by myself and does not represent
pete.forman at westerngeco.com    -./\.-   opinion of Schlumberger, Baker
http://petef.port5.com           -./\.-   Hughes or their divisions.



More information about the Python-list mailing list