Loading...

Python Remove Set

Python Remove Sets Items

In Python, sets are mutable collections that allow you to add and remove items. This lesson will cover various methods to remove items from a set, including using remove(), discard(), pop(), and clear(), as well as deleting an entire set with the del keyword.

1. Remove an Item

To remove an item from a set, you can use either the remove() or discard() method.

Example: Remove an Item Using remove()

The remove() method will raise an error if the item you want to remove does not exist in the set.

python
10 lines
|
81/ 500 tokens
1
2
3
4
5
6
7
8
9
10
# Creating a set of vehicles
vehicles_set = {"car", "bike", "bus"}

# Removing "bike" using the remove() method
vehicles_set.remove("bike")

print(vehicles_set)  # Output: {'car', 'bus'}

# Attempting to remove "bike" again will raise an error
# vehicles_set.remove("bike")  # Uncommenting this line will raise a KeyError
Code Tools

Example: Remove an Item Using discard()

The discard() method will not raise an error if the item you want to remove does not exist.

python
10 lines
|
71/ 500 tokens
1
2
3
4
5
6
7
8
9
10
# Creating a set of colors
colors_set = {"red", "green", "blue"}

# Removing "green" using the discard() method
colors_set.discard("green")

print(colors_set)  # Output: {'red', 'blue'}

# Discarding "green" again will not raise an error
colors_set.discard("green")  # No error occurs
Code Tools

2. Remove a Random Item

You can use the pop() method to remove a random item from the set. Since sets are unordered, you won't know which item gets removed. The return value of pop() is the item that was removed.

Example: Remove a Random Item Using pop()

python
9 lines
|
79/ 500 tokens
1
2
3
4
5
6
7
8
9
# Creating a set of shapes
shapes_set = {"circle", "square", "triangle"}

# Removing a random item using the pop() method
removed_shape = shapes_set.pop()

print(f"Removed shape: {removed_shape}")  # Output: Removed shape: triangle (or another shape)

print(shapes_set)  # Output: Remaining shapes, order may vary
Code Tools

3. Clear the Set

To remove all items from a set, you can use the clear() method. This method empties the set but does not delete it.

Example: Clear the Set

python
7 lines
|
37/ 500 tokens
1
2
3
4
5
6
7
# Creating a set of numbers
numbers_set = {1, 2, 3, 4}

# Clearing the set
numbers_set.clear()

print(numbers_set)  # Output: set() (an empty set)
Code Tools

4. Delete the Entire Set

If you want to delete the entire set, you can use the del keyword. This removes the set completely from memory.

Example: Delete the Set Completely

python
11 lines
|
75/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
# Creating a set of letters
letters_set = {"a", "b", "c"}

# Deleting the set
del letters_set

# Attempting to print the set will raise an error
try:
    print(letters_set)  # This will raise a NameError
except NameError:
    print("The set has been deleted.")  # Output: The set has been deleted.
Code Tools

Frequently Asked Questions

You can clear a set in Python using the clear() method, which removes all the elements from the set.

The remove() method raises a KeyError if the item doesn't exist in the set, whereas discard() removes the item without raising an error, even if the item is not found.

You can delete a set using the del keyword in Python. For example, del my_set will delete the set completely.

You can remove a set of elements from a list by using list comprehension or the filter() function to exclude elements found in the set.

Still have questions?Contact our support team