Python - Set Methods Flashcards
Return - none
Args - ( value to remove )
___________
searches for the given element in the set and removes it.
If the element(argument) passed to the remove() method doesn’t exist, keyError exception is thrown.
set.remove(element)
Return - none
Args - ( value to add )
___________
adds a given element to a set. If the element is already present, it doesn’t add any element.
set.add(element)
Return - none
Args - none
___________
makes a shallow copy. It doesn’t return any value.
set.copy()
Return - none
Args - none
___________
removes all elements from the set.
set.clear()
Return - set
Args - ( the set you wanna subtract out )
___________
returns the set difference of two sets.
A.difference(B)
Return - none
Args - ( value to remove from set )
___________
removes a specified element from the set (if present).
s.discard(x)
Return - set intersection
Args - ( sets to intersect with )
___________
returns the intersection of set A with all the sets (passed as argument).
A.intersection(*other_sets)
Return - bool
Args - ( second set or any iterable )
___________
returns True if two sets are disjoint sets. If not, it returns False.
You can also pass an iterable (list, tuple, dictionary and string) to disjoint(). The isdisjoint() method will automatically convert iterables to set and checks whether the sets are disjoint or not.
set_a.isdisjoint(set_b)
Return - bool
Args - ( superset you are taking subset from )
___________
returns True if all elements of a set are present in another set (passed as an argument). If not, it returns False.
A.issubset(B)
Return - bool
Args - ( subset )
___________
returns True if a set has every elements of another set (passed as an argument). If not, it returns False.
A.issuperset(B)
Return - random value that is popped
Args - none
___________
removes an random/arbitrary element from the set and returns the element removed.
set.pop()
Return - new set
Args - ( second set )
___________
returns a new set which is the symmetric difference of two sets.
The symmetric difference of two sets A and B is the set of elements which are in either of the sets A or B but not in both.
A.symmetric_difference(B)
Return - new set that is union of all sets
Args - ( other sets )
___________
returns a new set with distinct elements from all the sets.
A.union(*other_sets)
Return - none
Args - ( value to add to set, can be an iterable )
___________
adds elements from a set
A.update(B)
Return - bool
Args - ( iterable )
___________
returns True if any element of an iterable is True. If not, any() returns False.
any(iterable)