Python Glossary Русский

all Python 3Sets (set)function

set.pop

removal

Remove and return an arbitrary element; raises KeyError if the set is empty.

Syntax

s.pop()

Arbitrary does not mean random: which element leaves is decided by the hash-table layout, and for small sets of integers it usually looks like the smallest one. For a genuinely random pick convert first — random.choice(list(s)); random.sample stopped accepting sets in Python 3.11. pop itself shines when draining a set in a while s loop.

Examples

s = {1, 2, 3}
x = s.pop()
print(x in {1, 2, 3})   # → True
print(len(s))           # → 2

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy