Post

Python Set pop() Method

In this tutorial, we will understand about the python set pop() method and its uses.

Python Set pop() Method

The Python set pop() method removes and returns an arbitrary element from a set. Since sets are unordered, there’s no way to determine which element will be removed. If the set is empty, pop() raises a KeyError.

The syntax of the pop() method is:

1
set.pop()

Python set pop() Parameters

The pop() method doesn’t take any parameters. It simply removes and returns a random element from the set.

Here are examples demonstrating the pop() method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Example 1: Basic pop usage
numbers = {1, 2, 3, 4}
removed = numbers.pop()
print(removed)      # Output: arbitrary element
print(numbers)      # Output: remaining elements

# Example 2: Popping from mixed set
mixed = {'apple', 1, (2, 3)}
element = mixed.pop()
print(element)      # Output: arbitrary element
print(mixed)        # Output: remaining elements

# Example 3: Handling empty set
empty_set = set()
try:
    empty_set.pop()
except KeyError:
    print("Set is empty")  # Output: Set is empty

The pop() method is useful when you need to remove elements from a set one by one.

This post is licensed under CC BY 4.0 by the author.