Python Set Methods Flashcards
set()
creates an empty set.
add()
Adds an element to the set.
remove()
Removes a specified element from the set. Raises keyError if not found.
discard()
removes a specific element from the set(does nothing if nothing found)
pop()
Removes and returns a random element from the set
union()
Returns a new set by combining both sets
intersection()
Returns a new set with elements common to both sets.
difference()
Returns a new set with elements in the first set but not in the second set.
symmetric-difference()
Returns a new set with elements in either set but not in both.
len()
checking length
in/ not in
Checking if element is in the set
copy()
Returns a shallow copy of the set
clear()
Removes all elements from the set.