What is discard method in sets | How to use discard method of sets.

Definition:

This method is used to remove the particular item from the set, by taking that item as argument. Since the item is discarded from the ordinal set, therefore all changes are done in original set itself.

Important part of this method is that it does not throw an error if specified item is not present inside set.

Syntax:

set.dicard(element)

This method takes an argument.

Working

What is method in sets in python

output

Code

  1. A set is taken
  2. discard() method is being applied to it and an item is passed
  3. set is printed after discarding the particular element

Output

  1. The set without apple value is printed.

Interview Questions

  • What is the difference between discard method and remove method in set?

discard() method removes the specified item from set but if the specified item is not present it does not throw an error. remove() method is used to remove and delete the specified item but if the specified item is not present it throws an error.

Quick Links:

What is set in python? And how do we use it?

What is the difference_update method in sets in python | How to use the difference_update method of sets?

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

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