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.
Add an element to a set - Python Tutorial
From the course: Python Data Structures: Sets and Frozen Sets
Add an element to a set
- [Instructor] You can add an element to the original set. So how would you add an element to an empty set? Let keywords be our empty set. And then inside keywords, let us add some keywords. So I add keywords.add, import is a keyword. And then again, we can do keywords.add. Class is a keyword and then we can simply print keywords. As we have already written how to add elements in the set, we can remove the todo comment and then add program. You can see now the keyword set consists of import and class. Initially keywords was an empty set. Now it consists of import and class. How to add elements to a pre-populated set. Lets let numbers be set and it be 1, 2, 3, 4, 5. And now I'm interested in adding 10 to numbers. So numbers.add, you can simply write 10 and then do print numbers. Here also there is no need of using f string. We can just have keywords and then we can hear also just print numbers. We have already returned the example so we can remove comment on line 11. Let us run the…
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)
-
-
-
-
-
-