Access the sole element of a set [duplicate]

Use set.pop:

>>> {1}.pop()
1
>>>

In your case, it would be:

return S.pop()

Note however that this will remove the item from the set. If this is undesirable, you can use min|max:

return min(S) # 'max' would also work here

Demo:

>>> S = {1}
>>> min(S)
1
>>> S
set([1])
>>> max(S)
1
>>> S
set([1])
>>> 

Leave a Comment