Remove an arbitrary element
Write a Python program to remove and return an arbitrary element with pop().
{10, 20, 30}True 2
Approach
pop()removes some element; you cannot choose which.
Solution
s = {10, 20, 30}item = s.pop()print(item in {10, 20, 30}, len(s))True 2
Line by line
Which element comes out is an implementation detail, so the program checks the result instead of printing a value that could differ.
For a truly random choice, use random.choice(list(s)).
Learn the concept
New to this topic? Read the lesson, then come back.