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() - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Add an element to a set3m 39s
-
(Locked)
Add multiple elements with the update() method2m 38s
-
(Locked)
Remove an element from a set6m 57s
-
(Locked)
Remove an element from a set using discard()3m 15s
-
(Locked)
Using pop() to remove and return an element from a set3m 9s
-
(Locked)
Clearing all elements from a set42s
-
(Locked)
Solution: Filter by the first character1m 34s
-
(Locked)
-
-
-
-
-
-