All Methods
Lesson 8 of 11 in Coddy's Sets in Python course.
Here is a recap table of all the common methods and operations for sets in Python:
| Method | Shortcut | Description |
|---|---|---|
add() | Adds an element to the set. | |
clear() | Removes all elements from the set. | |
copy() | Returns a shallow copy of the set. | |
difference() | - | Returns the difference between two or more sets as a new set. |
discard() | Removes a specified element from the set if present. | |
intersection() | & | Returns the intersection of two or more sets as a new set. |
isdisjoint() | Returns True if two sets have no intersection. | |
issubset() | Returns True if another set contains this set. | |
issuperset() | Returns True if this set contains another set. | |
pop() | Removes and returns an arbitrary element from the set. | |
remove() | Removes a specified element from the set. Throws a KeyError if the element is not found. | |
symmetric_difference() | ^ | Returns the symmetric difference of two sets as a new set. |
union() | | | Returns the union of sets as a new set. |
update() | Updates the set with the union of itself and others. Can accept lists, tuples, strings, or other sets. |
Let's challenge the knowledge!
Try it yourself
This lesson doesn't include a code challenge.