CKTIS / Python
set.pop
Removes and returns an arbitrary set member.
Version: Python 3.14; repository image, production runtime not attested
Syntax
s.pop()Removes and returns an arbitrary set member.
Selection order is unspecified. Test membership and size instead of relying on a particular element.
With {2, 3}, either value may be selected. The checks show that the returned value was in the original set, is now absent and leaves one remaining member. This tests the mutation without relying on iteration order. An empty set raises KeyError.
s = {2, 3}
x = s.pop()
assert x in {2, 3} and x not in s and len(s) == 1
Examples
s = {2, 3}
value = s.pop()
print(value in {2, 3}, value in s, len(s))
Result
True False 1
With {2, 3}, either value may be selected. The checks show that the returned value was in the original set, is now absent and leaves one remaining member. This tests the mutation without relying on iteration order. An empty set raises KeyError.