Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pierian-data

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: pierian-data/complete-python-3-bootcamp
Path: blob/master/17-Advanced Python Objects and Data Structures/03-Advanced Sets.ipynb
Views: 648
Kernel: Python 3

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.

s = set()

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!)

s.add(1)
s.add(2)
s
{1, 2}

clear

removes all elements from the set

s.clear()
s
set()

copy

returns a copy of the set. Note it is a copy, so changes to the original don't effect the copy.

s = {1,2,3} sc = s.copy()
sc
{1, 2, 3}
s
{1, 2, 3}
s.add(4)
s
{1, 2, 3, 4}
sc
{1, 2, 3}

difference

difference returns the difference of two or more sets. The syntax is:

set1.difference(set2)

For example:

s.difference(sc)
{4}

difference_update

difference_update syntax is:

set1.difference_update(set2)

the method returns set1 after removing elements found in set2

s1 = {1,2,3}
s2 = {1,4,5}
s1.difference_update(s2)
s1
{2, 3}

discard

Removes an element from a set if it is a member. If the element is not a member, do nothing.

s
{1, 2, 3, 4}
s.discard(2)
s
{1, 3, 4}

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.)

s1 = {1,2,3}
s2 = {1,2,4}
s1.intersection(s2)
{1, 2}
s1
{1, 2, 3}

intersection_update will update a set with the intersection of itself and another.

s1.intersection_update(s2)
s1
{1, 2}

isdisjoint

This method will return True if two sets have a null intersection.

s1 = {1,2} s2 = {1,2,4} s3 = {5}
s1.isdisjoint(s2)
False
s1.isdisjoint(s3)
True

issubset

This method reports whether another set contains this set.

s1
{1, 2}
s2
{1, 2, 4}
s1.issubset(s2)
True

issuperset

This method will report whether this set contains another set.

s2.issuperset(s1)
True
s1.issuperset(s2)
False

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.)

s1
{1, 2}
s2
{1, 2, 4}
s1.symmetric_difference(s2)
{4}

union

Returns the union of two sets (i.e. all elements that are in either set.)

s1.union(s2)
{1, 2, 4}

update

Update a set with the union of itself and others.

s1.update(s2)
s1
{1, 2, 4}

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!