• Python Basics

  • Python Variables

  • Operators in Python

  • Conditional Statements in Python

  • Python Lists

  • Python Tuples

  • Python Sets

  • Python Dictionaries

  • Loops in Python

  • Python Arrays and Functions

  • Conclusion

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
1# Creating a set of vehicles
2vehicles_set = {"car", "bike", "bus"}
3
4# Removing "bike" using the remove() method
5vehicles_set.remove("bike")
6
7print(vehicles_set)  # Output: {'car', 'bus'}
8
9# Attempting to remove "bike" again will raise an error
10# vehicles_set.remove("bike")  # Uncommenting this line will raise a KeyError

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
1# Creating a set of colors
2colors_set = {"red", "green", "blue"}
3
4# Removing "green" using the discard() method
5colors_set.discard("green")
6
7print(colors_set)  # Output: {'red', 'blue'}
8
9# Discarding "green" again will not raise an error
10colors_set.discard("green")  # No error occurs

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
1# Creating a set of shapes
2shapes_set = {"circle", "square", "triangle"}
3
4# Removing a random item using the pop() method
5removed_shape = shapes_set.pop()
6
7print(f"Removed shape: {removed_shape}")  # Output: Removed shape: triangle (or another shape)
8
9print(shapes_set)  # Output: Remaining shapes, order may vary

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
1# Creating a set of numbers
2numbers_set = {1, 2, 3, 4}
3
4# Clearing the set
5numbers_set.clear()
6
7print(numbers_set)  # Output: set() (an empty set)

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
1# Creating a set of letters
2letters_set = {"a", "b", "c"}
3
4# Deleting the set
5del letters_set
6
7# Attempting to print the set will raise an error
8try:
9    print(letters_set)  # This will raise a NameError
10except NameError:
11    print("The set has been deleted.")  # Output: The set has been deleted.

Frequently Asked Questions