Python Set discard() Method – Tutorial with Examples

Python Set discard() Method

Python Set is an unordered and mutable collection of unique elements. The elements in a set are not indexed, so we cannot access them by their position. In order to access the elements, we can use the built-in methods provided by the Set object. One such method is the discard() method, which removes a specific element from the Set if it exists in the Set.

Syntax

set.discard(element)

The discard() method takes an argument ‘element’ which is the element to be removed from the Set. If the element exists in the Set, it will be removed. If the element does not exist in the Set, no action is taken.

The discard() method is similar to the remove() method. The only difference is that the remove() method raises a KeyError if the element is not found in the Set, whereas the discard() method does not raise any error.

Examples

Example 1: Using discard() method on a Set

Let’s create a Set and use the discard() method to remove an element from the Set:

# Creating a Set
my_set = set([1, 2, 3, 4, 5])

# Removing an element using discard() method
my_set.discard(3)

# Printing the Set after removal
print(my_set)

Output:

{1, 2, 4, 5}

In the above example, we created a Set my_set with elements [1, 2, 3, 4, 5]. We used the discard() method to remove the element ‘3’ from the Set. After removal, the Set contains the elements [1, 2, 4, 5].

Example 2: Removing a non-existent element using discard()

In this example, we will use the discard() method to remove an element that does not exist in the Set:

# Creating a Set
my_set = set([1, 2, 4, 5])

# Removing a non-existent element using discard() method
my_set.discard(3)

# Printing the Set after removal
print(my_set)

Output:

{1, 2, 4, 5}

In the above example, we created a Set my_set with elements [1, 2, 4, 5]. We used the discard() method to remove the element ‘3’ from the Set, which does not exist in the Set. As a result, no action is taken, and the Set remains the same.

Example 3: Using discard() method in a loop

In this example, we will use the discard() method to remove multiple elements from a Set in a loop:

# Creating a Set
my_set = set([1, 2, 3, 4, 5])

# Removing multiple elements using discard() method in a loop
for i in range(1, 4):
    my_set.discard(i)

# Printing the Set after removal
print(my_set)

Output:

{4, 5}

In the above example, we created a Set my_set with elements [1, 2, 3, 4, 5]. We used the discard() method to remove the elements 1, 2, and 3 from the Set using a for loop. After removal, the Set contains the elements [4, 5].

Use Cases

The discard() method can be used in situations where we want to remove an element from the Set if it exists, without raising an error if it does not exist. It can be used to remove a single element or multiple elements in a loop. The discard() method can also be used to simplify code by avoiding if-else statements to check if an element exists in the Set before removal.

Overall, the discard() method is a useful tool to have in our Python toolkit for manipulating Sets, and can come in handy in a wide range of use cases.

Leave a Reply

Your email address will not be published. Required fields are marked *