Python

discard method in set

Click for All Topics

discard method in set - Python

  • Discard method is used to delete or remove an element from a set.
  • This method takes exactly one argument.
  • When we call the discard method and pass an element as an argument Python will calculate the hash code of that element using the hash function.
  • This hash code is used to check whether an element is present in a set or not.
  • If an element is present with the same hash code it will be removed from the set.
  • If an element is not present in the set it will not give an error.

Syntax

set_name.discard(element)

Parameter –

  • element → element which you want to remove.
  • return value → none.

Example 1- Discard an element that is present in the set

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

# Output -
# {1,2,3,4} # my_set before removing element
# {1,2,4}   # my_set after removing element
				
			
  • In the above example, first we create a set (namely as my_set) with elements 1,2,3,4.
  • After that, we print the set in the output screen using the print() function.
  • After that, we discard element 3 from my_set using the discard method.
  • And then print modified set(my_set).

Example 2- Discard an element that does not exist in the set

				
					my_set=set()  # empty set
for i in range(1,6):
	my_set.add(i)
print(my_set)
my_set.discard(7)
print(my_set)

# Output -
# {1, 2, 3, 4, 5} # before removing element my_set
# {1, 2, 3, 4, 5} # after removing element my_set
				
			
  • In the above example, first we create an empty set(my_set) by using the set() function.
  • After that, we iterate a for-loop in the range 1-6 and add elements in my_set using the add() method.
  • After adding elements to the set we are trying to remove 7 from my_set that does not exist in it.
  • We can see that this line(line – 5) does not give any error.

Example 3- Discard even element from a set

				
					s={10,11,12,13,14,15,16,17,18,19}
s1={10,11,12,13,14,15,16,17,18,19}
print(s)
for i in s1:
	if i%2==0:
		s.discard(i)
print(s)

# Output -
# {10, 11, 12, 13, 14, 15, 16, 17, 18, 19} # before discard element from set s
# {11, 13, 15, 17, 19}                     # after discard element from set s
				
			

Example 4- Discard string from a set

				
					original_set={'hello','this', 'is', 'edSlash'}
print(original_set)
discarded_element=original_set.discard('hello')
print(discarded_element)
print(original_set) 

# Output -
# {'hello','this','is','edSlash'}
# None
# {'this', 'is', 'edSlash'}
				
			
  • In the above example, we can see that the discard method returns None.

Example 5- Discard method trying to pass two arguments

				
					s={'I','am','anurag', 'dubey'}
s.discard('anurag','dubey')
print(s)

# Output - 
# Traceback (most recent call last):
 #  File "/home/main.py", line 2, in <module>
    # s.discard('anurag','dubey')
# TypeError: set.discard() takes exactly one argument (2 given)

				
			
  • In the above example, we can see that we can pass only one argument to the discard method.