3.4. Sets#
Amanda R. Kube Jotte
A set is like a dictionary with just the keys and no values. It stores unique elements, with no duplicates and no ordering of the elements.
Creating Sets#
Sets, like dictionaries, are created with curly braces {}
. Instead of having pairs of keys and values in the braces, we add the individual elements separated by commas.
my_set = {1, 2, 3}
my_set
{1, 2, 3}
Duplicate items are automatically removed.
my_set = {1, 2, 2, 3}
my_set
{1, 2, 3}
Note
If you want to create an empty set, you have to use the set()
function. The following code:
{}
…will create an empty dictionary.
As sets are unordered, we cannot index them like we could lists or tuples.
my_set[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 my_set[0]
TypeError: 'set' object is not subscriptable
Membership Operators#
We typically use sets to keep track of membership in a group. We can test membership using the boolean in
operator. The in
operator is one of two membership operators, the other being not in
. These test whether or not a value is in a container.
3 in my_set
5 in my_set
2 not in my_set
These operators also work with lists.
primes_abridged = [2, 3, 5, 7, 11, 13, 17, 19, 23]
4 in primes_abridged
And they can be used to check if certain keys are in
a dictionary.
addresses = {
"Bill": {'phone':3125551234, 'email': 'bill@internet.com'},
"Mary": {'phone':3125559876, 'email': 'mary@web.net'},
"Sue": {'phone':3125552468, 'email': 'sue@email.edu'}
}
'Sue' in addresses
"Annie" not in addresses
Mutability and Methods#
Like lists, sets are mutable. You can add()
or remove()
elements from sets using set methods.
my_set.add(4)
my_set.remove(2)
In mathematics, there are several common operations we perform on sets. In Python, these operations are available as both methods and as operators. The most common are union, intersection, and difference.
Union: Combines two sets, returning a set that contains every element that appears in either set.
The union combines two sets, returning a set that contains every element that appears in either set. We can perform a union using union()
or by using the bitwise operator |
.
a = {1, 2, 3}
b = {3, 4, 5}
a.union(b) # or a | b
The intersection returns only the elements that the two sets have in common. We can perform an intersection using intersection()
or by using the bitwise operator &
.
a.intersection(b) # or a & b
The difference returns the elements in the first set that are not in the second set. We can take a difference using difference()
or by using the operator -
.
a.difference(b)
b.difference(a)
Note
Order doesn’t matter when using intersection or union - a.union(b)
gives the same result as b.union(a)
.
However, it does matter when taking a set difference. Above, we can see that a.difference(b)
is not equal to b.difference(a)
.
Sets can be useful when working with mathematical-style sets, like calculating probabilities. They are also handy for fast membership checks, and for removing duplicates from data.
We have now covered lists, tuples, dictionaries, and sets. These are all built-in data structures in Python, which means we can use them without importing any additional libraries.
In the next section, we will work with an extremely useful and versatile object called an array. To do this, we will need to learn a new library: numpy
.