Python Set pop()

The pop() method removes an arbitrary element from the set and returns the element removed.

The syntax of pop() for sets is:


pop() Parameters

The pop() method doesn't take any arguments.

Return Value from pop()

The pop() method returns an arbitrary (random) element from the set. Also, the set is updated and will not contain the element (which is returned).

If the set is empty, TypeError exception is raised.

Example: How pop() works for Python Sets?

A ={'a', 'b', 'c', 'd'}

print('Return Value is', A.pop())
print('A = ', A)

When you run the program, we got the following output

Return Value is d
A =  {'a', 'b', 'c'}

Note: You may get different output as pop() returns and removes a random element.

Did you find this article helpful?