Python Set pop() Method
set.pop() removes and returns one item from a set.
This method is useful when you want to take out any item, but you do not care which one. That is important because sets are unordered, so pop() does not remove an item by position.
Quick example
items = {"apple", "banana", "cherry"}
removed = items.pop()
print(removed)
print(items)
What this does:
- Removes one item from
items - Stores that removed item in
removed - Prints the removed item and the updated set
Important: set.pop() removes and returns one item from the set. The removed item is not chosen by index.
What set.pop() does
set.pop():
- Removes one item from a set
- Returns the removed item
- Works only on non-empty sets
- Does not take an index or value argument
If you are new to sets, see Python sets explained.
Basic syntax
my_set.pop()
Key points:
- The syntax is
my_set.pop() - It takes no arguments
- If you need the removed item, save the return value in a variable
Example:
colors = {"red", "blue", "green"}
item = colors.pop()
print(item)
print(colors)
Important behavior to understand
Sets are unordered collections.
That means:
- You cannot choose which item
pop()removes - The removed item may seem different across runs
- You should not use
pop()when you need to remove a specific value
For example, this is not valid:
fruits = {"apple", "banana", "cherry"}
fruits.pop("apple")
This causes an error because set.pop() does not accept a value.
If you want to remove a specific item, use set.remove() or set.discard().
Example: remove one item
Here is a simple example:
animals = {"cat", "dog", "rabbit"}
removed_animal = animals.pop()
print("Removed:", removed_animal)
print("Updated set:", animals)
Possible output:
Removed: dog
Updated set: {'cat', 'rabbit'}
Your output may be different because sets do not guarantee order.
What happens with an empty set
Calling pop() on an empty set raises a KeyError.
Example:
items = set()
items.pop()
This raises:
KeyError: 'pop from an empty set'
If needed, you can learn more about this kind of problem on the KeyError in Python causes and fixes page.
Check before calling pop()
A simple way to avoid the error is to check whether the set has items:
items = set()
if items:
removed = items.pop()
print("Removed:", removed)
else:
print("The set is empty.")
Use try and except
You can also handle the error directly:
items = set()
try:
removed = items.pop()
print("Removed:", removed)
except KeyError:
print("Cannot pop from an empty set.")
When to use pop() vs remove() vs discard()
These three methods all remove items from a set, but they are used in different situations.
Use pop()
Use pop() when:
- You want to remove any one item
- You do not care which item is removed
- You want the removed item returned
numbers = {1, 2, 3}
value = numbers.pop()
print(value)
Use remove()
Use remove() when:
- You want to delete a specific item
- You expect that item to be in the set
numbers = {1, 2, 3}
numbers.remove(2)
print(numbers)
If the item is missing, remove() raises an error. See the set.remove() method for details.
Use discard()
Use discard() when:
- You want to delete a specific item
- You do not want an error if the item is missing
numbers = {1, 2, 3}
numbers.discard(5)
print(numbers)
This does nothing if 5 is not in the set. See the set.discard() method for more examples.
Common mistakes
Beginners often run into these problems with set.pop():
- Trying to pass a value like
my_set.pop("apple") - Expecting
pop()to remove the first or last item - Calling
pop()on an empty set - Using
pop()when you need to remove a specific item
Useful checks while debugging:
print(my_set)
print(len(my_set))
print(type(my_set))
item = my_set.pop()
These help you confirm:
- What values are in the set
- Whether the set is empty
- That the object is really a set
- What value was actually removed
FAQ
Does set.pop() remove the first item?
No. Sets do not keep order in the way lists do, so pop() removes an arbitrary item.
Can I use an index with set.pop()?
No. Unlike list pop(), set pop() does not accept an index.
What error happens if the set is empty?
Python raises KeyError when pop() is called on an empty set.
How do I remove a specific item from a set?
Use remove() or discard() instead of pop().