Set
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Set
Container of unique values.
Curly brackets are used as the notations for sets.
We can create a set out of any sequence of python objects. Even if we pass a lot of repeated values, only the unique values will be stored inside the set.
our_set = {1,1,1,1,1,1,1,1,0,0,0,0,0,1,2,2,4}
our_set{0, 1, 2, 4}our_set2 = {0,0,0, True, True, False, "some string"}
our_set2{0, True, 'some string'}Lets say we have a list with multiple repetative values inside it and we want only the unique values from it.
list1 = [0, 1, 2, 2, 0, 2, 1, 5, 1]
list1[0, 1, 2, 2, 0, 2, 1, 5, 1]set1 = set(list1)
set1{0, 1, 2, 5}Sets also have the curly bracket notation, same as the dictionaries. But while dictionaries store values as a key value pair, set just keeps the unique values.
Like we noticed in the above code cell, when using the set function, we kept the unique values and stored them as variable set1
Sets and dictionaries can not be indexed with their location indexes like we do in case of lists and tuples because sets and dictionaries are unordered.
Sets and dictionaries just keep unique values and unique keys respectively.
# this will throw an error
# set1[0]
# TypeError: 'set' object is not subscriptableUseful set methods
set1 = {0,1,2}
set1{0, 1, 2}add
set1.add(10)
set1{0, 1, 2, 10}discard
Delete a particular set element
set1.discard(10)
set1{0, 1, 2}Mathematical operations with Sets
set1 = {0,1,2,3}
set1{0, 1, 2, 3}set2 = {2,3,4,5}
set2{2, 3, 4, 5}Here we will use the methods available inside the class set.
Intersection
Lets find the values common in two sets
set.intersection(set1, set2){2, 3}2 and 3 are present inside both the sets, set1 and set2. That's the intersection operation.
Union
Lets check the overall unique values if we combine two sets
set.union(set1, set2){0, 1, 2, 3, 4, 5}Union gives us the values which are available if we combine the two sets.
Difference
Which values of first set are unique to only the first set
# We haven't really discussed function arguments
# so for now, consider set1 if the first set and
# set2 is the second set
set.difference(set1, set2)
# Output will be
# how many of the set1 values are present only in
# set1 and not in set2{0, 1}set.intersection(set1, set2){2, 3}set.difference(set2, set1){4, 5}Universal set
Lets create a universal set which contains all the values
Then we will create subsets from it
universal_set = set(range(10))
universal_set{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}subset1 = {0,1,2,7}
subset1{0, 1, 2, 7}subset2 = {5,6,7,8}
subset2{5, 6, 7, 8}Lets confirm if all values of subset1 are present in the universal_set.
set.issubset(subset1, universal_set)TruePython says, yes all the values of subset1 are available inside the universalset. So, the relationship between the two sets is of a parent and a child. universalset being the parent and subset1 being the child.
universal_set being a parent could have multiple children.
subset2 is another child of universal_set
set.issubset(subset2, universal_set)TrueThis true means that yes all of the values of subset2 are present in the universal_set.
subset3 = {11, 1, 2, 5}
subset3{1, 2, 5, 11}Now lets check if this subset3 is a child of the universal set or not.
set.issubset(subset3, universal_set)FalseNow python says, subset3 contains some values which are not present in the supposedly universalset. So, in this case subset3 is not the child of this universalset.
To be honest, as you might have figured, sets are used for mathematical set operation.
So, let's move on from this topic to focus more on python for now.
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →