Python Set pop() method

The Python Set pop() method is used to get a random arbitrary element from the set and the value returned will be removed from the input set. If the set is empty, it raises a KeyError.

Note that set.pop() is a mutating operation, which means that it modifies the original set in place. If you want to remove an element from a set without modifying the set, you can use the set.discard() method instead.

1. Quick Examples of Set pop()

Following are quick examples of how to use pop() method in python.


# Quick Examples

# Example 1: Use pop() to remove any element
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)
popped=myset.pop()
print("Popped item: ",popped)
print("Final set:",myset)

# Example 2: Use pop() to remove any element
myset = {"hello","python","panda","scipy"}
print("Actual set:",myset)
popped=myset.pop()
print("Popped item: ",popped)
print("Final set:",myset)

# Example 3: Pop an element from an empty set
s = set()
try:
    s.pop()
except KeyError:
    print("The set is empty")

2. Set pop() Method in Python

In Python, the set.pop() method removes and returns an arbitrary element from the set. If the set is empty, TypeError is thrown. So make sure that set contains at least one element to pop. It will not take any parameters.

Keep in mind that set.pop() is a mutating operation, which means that it modifies the set in place. If you want to remove an element from a set without modifying the set, you can use the set.discard() method instead.

2.1 Set pop() Syntax


# Here, myset is the input set.
myset.pop()

3. Examples

Let’s create a set with some integers and use pop() method to remove one element randomly.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Use pop() to remove any element
popped=myset.pop()
print("Popped item: ",popped)
print("Final set:",myset)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# Popped item:  1
# Final set: {34, 3, 67, 6, 12, 45, 56, 89}

The above example removes an element 1 from the set and this element has been removed from the original set.

Let’s create a set with some strings and use the pop() method to remove one element randomly. Here, it removes the element pandas from the set.


# Consider the set with some strings
myset = {"hello","python","panda","scipy"}
print("Actual set:",myset)

# Use pop() to remove any element
popped=myset.pop()
print("Popped item: ",popped)
print("Final set:",myset)

# Output:
# Actual set: {'panda', 'scipy', 'hello', 'python'}
# Popped item:  panda
# Final set: {'scipy', 'hello', 'python'}

4. Use pop() on Empty Set

Let’s create an empty set and try to remove the item using pop(). As specified above calling pop() on empty Set returns an error.


# Consider an empty set
myset = {}
print("Actual set:",myset)

# Pop element
popped=myset.pop()
print("Popped item: ",popped)

Yields below error.

python set pop

If you don’t want to throw an error, use the below approach. Here, we are using try except to catch KeyError and print the message.


# Pop an element from an empty set
s = set()
try:
    s.pop()
except KeyError:
    print("The set is empty")

# Output:
# The set is empty

5. Conclusion

pop() in python Set is used to remove a random element from the set. If the set is empty, TypeError is thrown. So make sure that set contains at least one element to pop or at least use exception handling to catch the error.

Leave a Reply