From the course: Python Data Structures: Sets and Frozen Sets

Unlock this course with a free trial

Join today to access over 24,100 courses taught by industry experts.

Remove an element from a set using discard()

Remove an element from a set using discard() - Python Tutorial

From the course: Python Data Structures: Sets and Frozen Sets

Remove an element from a set using discard()

- [Instructor] Another way to remove an element from a set is by using discard method. On line 10, we have a set named as country and countries present in set are USA, Canada, UK, Australia. Now how would you remove Australia from it? Let us try removing Australia from our set. So before we remove Australia, let us write here Value set before discarding Australia. And here we can write countries. countries is our set. countries.discard. Discard is a built-in method present in set class, and inside the parenthesis you can pass the element that you want to remove. So we are trying to remove Australia. You can just pass in "Australia". And here let us, again, print("Value of set after using discard method ", countries) We have already written the function so we can remove line 5. Let us run the program and see what is the output. python our file name, python3 discard.py is our file name. Let us run the program. So, Value of set before discarding Australia, before that, the value is…

Contents