Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/17-Advanced Python Objects and Data Structures/03-Advanced Sets.ipynb
Views: 648
Advanced Sets
In this lecture we will learn about the various methods for sets that you may not have seen yet. We'll go over the basic ones you already know and then dive a little deeper.
add
add elements to a set. Remember, a set won't duplicate elements; it will only present them once (that's why it's called a set!)
clear
removes all elements from the set
copy
returns a copy of the set. Note it is a copy, so changes to the original don't effect the copy.
difference
difference returns the difference of two or more sets. The syntax is:
For example:
difference_update
difference_update syntax is:
the method returns set1 after removing elements found in set2
discard
Removes an element from a set if it is a member. If the element is not a member, do nothing.
intersection and intersection_update
Returns the intersection of two or more sets as a new set.(i.e. elements that are common to all of the sets.)
intersection_update will update a set with the intersection of itself and another.
isdisjoint
This method will return True if two sets have a null intersection.
issubset
This method reports whether another set contains this set.
issuperset
This method will report whether this set contains another set.
symmetric_difference and symmetric_update
Return the symmetric difference of two sets as a new set.(i.e. all elements that are in exactly one of the sets.)
union
Returns the union of two sets (i.e. all elements that are in either set.)
update
Update a set with the union of itself and others.
Great! You should now have a complete awareness of all the methods available to you for a set object type. This data structure is extremely useful and is underutilized by beginners, so try to keep it in mind!
Good Job!