Python - Set Methods (Reversed) Flashcards
set.remove(element)
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.add(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.copy()
Return - none
Args - none
___________
makes a shallow copy. It doesn’t return any value.
set.clear()
Return - none
Args - none
___________
removes all elements from the set.
A.difference(B)
Return - set
Args - ( the set you wanna subtract out )
___________
returns the set difference of two sets.
s.discard(x)
Return - none
Args - ( value to remove from set )
___________
removes a specified element from the set (if present).
A.intersection(*other_sets)
Return - set intersection
Args - ( sets to intersect with )
___________
returns the intersection of set A with all the sets (passed as argument).
set_a.isdisjoint(set_b)
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.
A.issubset(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.issuperset(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.
set.pop()
Return - random value that is popped
Args - none
___________
removes an random/arbitrary element from the set and returns the element removed.
A.symmetric_difference(B)
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.union(*other_sets)
Return - new set that is union of all sets
Args - ( other sets )
___________
returns a new set with distinct elements from all the sets.
A.update(B)
Return - none
Args - ( value to add to set, can be an iterable )
___________
adds elements from a set
any(iterable)
Return - bool
Args - ( iterable )
___________
returns True if any element of an iterable is True. If not, any() returns False.