Saturday, August 30, 2025
HomeLanguagesPython Set discard() Function

Python Set discard() Function

Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value.

Example: In this example, we are removing the integer 3 from the set with discard() in Python.

Python3




my_set = {1, 2, 3, 4, 5}
my_set.discard(3
print(my_set)


Output

{1,2,4,5}

Python Set discard() Syntax

set.discard(element)

Parameter
element – an item to remove from the set.

Return Value
return  – discard() method doesn’t return any value.

Python Set discard() Examples

Discard() an item from a set that is present in the Python Set

In this example, we have a set and we use discard() to remove an existing integer “5” from the set using Python.

Python3




numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}
 
print(numbers)
 
# Deleting 5 from the set
numbers.discard(5)
 
# printing the resultant set
print(numbers)


Output

{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 6, 7, 8, 9}

Discard() an item from a set that is not present in the Python set

In this example, we have a set and we use discard() to remove a non-existing integer “13” from the set using Python.

Python3




numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}
 
print(numbers)
 
# passing an element that is not in set
numbers.discard(13)
# this will not throw any errors but set remains
# same as before
 
# printing the resultant set
print("\nresultant set : ", numbers)


Output

{1, 2, 3, 4, 5, 6, 7, 8, 9}
resultant set :  {1, 2, 3, 4, 5, 6, 7, 8, 9}

Discard() a String item from a set that is present in the Python Set

In this example, we have a set and we use discard() to remove an existing string “geek” from the set using Python.

Python3




myset = {'a', 1, "geek", 2, 'b', 'abc', "neveropen", 8}
 
print(myset)
 
# Deleting a from the set
myset.discard("geek")
 
# printing the resultant set
print(myset)


Output

{1, 2, 'b', 'a', 8, 'neveropen', 'abc', 'geek'}
{1, 2, 'b', 'a', 8, 'neveropen', 'abc'}

Discard() String item from a set that is not present in the Python Set

In this example, we have a set and we use discard() to remove a non-existing string “Lazyroar” from the set using Python.

Python3




myset = {'a', 1, "geek", 2, 'b', 'abc', "neveropen", 8}
 
print(myset)
 
# trying to Delete Lazyroarfrom the set which is not there
myset.discard("Lazyroar")
 
# printing the resultant set
print(myset)


Output

{1, 2, 'b', 'a', 8, 'neveropen', 'abc', 'geek'}
{1, 2, 'b', 'a', 8, 'neveropen', 'abc', 'geek'}

Note – To know the difference between discard() and remove() click here.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6732 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6703 POSTS0 COMMENTS