How to get an item from a simple set?

Pete Forman pete.forman at
Wed Nov 24 17:16:42 CET 2004

Skip Montanaro <skip at> 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    -./\.-   opinion of Schlumberger, Baker           -./\.-   Hughes or their divisions.

More information about the Python-list mailing list