As with other Python objects, there are the set has several functions and methods to make working with it easier.
As with the list, tuple, and dictionary, you can use the len()
function to get the number of elements within a set.
Adding and Removing Elements
Adding Elements
As with other mutable objects, you can add and remove elements from a set. For example, .add()
adds items to a set. However, if I have a second set, I can add it to the first set using the .update()
method.
set1 = set([1,2,3])
set1.add(4)
print(set1) # displays {1,2,3,4}
set1.add(2) # won't do anything because the set already has a element with a value of 2 in it.
print(set1) # displays {1,2,3,4}
set2 = set([6,7,8])
print(set2) # prints {6,7,8}
set1.update(set2) # appends set2 to set 1. Set 2 remains the same.
# no duplicates will be added to set1
Removing Elements
There are two basic ways to remove an element from a set: .remove()
and .discard()
.
With both of these, you will send an element to the method to remove as an argument. However, if the difference between these methods is what happens when an element cannot be found in the set.
With .discard()
, if the element doesn’t exist, nothing happens. With .remove()
, if the element doesn’t exist, the program raises a KeyError
which should be handled.
set1 = set([1,2,3])
print("getting ready to discard")
set1.discard(4)
print("getting ready to remove")
set1.remove(4)
Looping Through a Set
While not as common as other data collection objects, you can still loop through a set. As with the others, the for loop is a natural at handling this.
set1 = set([1,2,3,'a','b','c'])
for value in set1 :
print(value)
Finding Elements in a Set
Python’s in and not in operators work well to identify those elements which are in or not in a set. This returns a Boolean value, like they do with other collection objects, and can easily be used for conditional objects and conditional (while) loops.
Combining Sets – Unions
We looked at the .update()
method as a way to add one set to another existing set. However, sometimes you need the original sets to remain as they are, and to create a new set from the combination of those two.
This is where a .union()
method comes into use. When called, it takes an argument of a second set, and returns a new set. It leaves the first two sets alone and doesn’t change them.
In a mathematical union, you join two sets, but if there are duplicates, only one of those remains as the set cannot have multiple elements with the same value.
# create the initial sets
set1 = set([1,2,3,4])
set2 = set([6,7,8])
# run the union method
set3 = set1.union(set2)
# verify the results
print(set1) # displays {1,2,3,4}
print(set2) # prints {6,7,8}
print(set3) # prints {1,2,3,4,6,7,8}
There is a short cut to the union method. It’s the pipe character, which looks like a vertical line (|). This also performs the union operation between two sets.
# create the initial sets
set1 = set([1,2,3,4])
set2 = set([6,7,8])
# run the union method
set3 = set1 |set2
# verify the results
print(set1) # displays {1,2,3,4}
print(set2) # prints {6,7,8}
print(set3) # prints {1,2,3,4,6,7,8}
Finding Intersection Between Sets
From a mathematical stand point, an intersection of two (or more) sets, is where you find all the elements that they have in common. So if one set has 1, 2, 3, and 4 as elements, and the second set has 3, 4, 5, and 6, then the intersection of those two sets is 3 and 4.
If no elements are found in both sets, then you get an empty set – a set with no elements within it.
Python supports this idea with the .intersection()
method, which returns a new set as a result.
# create the initial sets
set1 = set([1,2,3,4,5])
set2 = set([3,4,5,6,7])
# run the intersection method
set3 = set1.intersection(set2)
# verify the results
print(set1) # displays {1,2,3,4,5}
print(set2) # prints {3,4,5,6,7}
print(set3) # prints {3,4,5}
Finding Differences in Sets
One of the things you will sometimes need to do is find the differences between a set. There are two different difference methods. The .difference()
method will find elements in set1, which are not in set2. The .symmetric_difference()
finds elements in set2 that are not in set1.
With both .symmetric_difference()
and .difference()
the differences are returned, so the original sets are unchanged.
# create the initial sets
set1 = set([1,2,3,4,5])
set2 = set([3,4,5,6,7])
set3 = set1.difference(set2)
set4 = set1.symmetric_difference(set2)
You can also use the -
operator to find the difference of two sets and the ^
to find the symmetric difference. However, the methods are probably easier for most people to read and work with, so they would be recommended.
Subsets and Supersets
When working with a two sets, there is often a need to see if one set is also totally found within the other set. If the first set is found in the second, it is said to be a sub set of the second, and the second a superset.
Generally the two sets are of different sizes, but they can be the same size.
Python has a .issubset()
and .issuperset()
methods, which return a Boolean value to let you know if they are a superset/subset.
# create the initial sets
set1 = set([1,2,3,4,5,6,7])
set2 = set([3,4,5])
print( set2.issubset(set1) ) # prints True
print( set1.isuperset(set2) ) # prints True
print( set2.isuperset(set1) ) # prints False
Python’s Set Methods and Functions was originally found on Access 2 Learn