Chapter 7 - Sets

This chapter will introduce a different kind of container: sets. Sets are unordered lists with no duplicate entries. You might wonder why we need different kinds of containers. We will postpone that discussion until chapter 8.

At the end of this chapter, you will be able to:

  • create a set
  • add items to a set
  • extract/inspect items in a set

If you want to learn more about these topics, you might find the following links useful:

If you have questions about this chapter, please refer to the forum on Canvas.

1. How to create a set

It's quite simple to create a set


In [ ]:
a_set = {1, 2, 3}
a_set

In [ ]:
empty_set = set() # you have to use set() to create an empty set! (we will see why later)
print(empty_set)
  • Sets are surrounded by curly brackets and the elements in the set are separated by commas
  • A set can be empty (use set() to create it)
  • Sets do not allow duplicates
  • sets are unordered (the order in which you add items is not important)
  • A set can only contain immutable objects (for now that means only strings and integers can be added)
  • A set can not contain mutable objects, hence no lists or sets

Please note that sets do not allow duplicates. In the example below, the integer 1 will only be present once in the set.


In [ ]:
a_set = {1, 2, 1, 1}
print(a_set)

Please note that sets are unordered. This means that it can occur that if you print a set, it looks different than how you created it


In [ ]:
a_set = {1, 3, 2}
print(a_set)

This also means that you can check if two sets are the same even if you don't know the order in which items were put in:


In [ ]:
{1, 2, 3} == {2, 3, 1}

Please note that sets can only contain immutable objects. Hence the following examples will work, since we are adding immutable objects


In [ ]:
a_set = {1, 'a'}
print(a_set)

But the following example will result in an error, since we are trying to create a set with a mutable object


In [ ]:
a_set = {1, []}

2. How to add items to a set

the most common way of adding an item to a set is by using the add method. The add method takes one positional argument, namely what you are going to add to the set and it returns None.


In [ ]:
a_set = set()
a_set.add(1)
print(a_set)

In [ ]:
a_set = set()
a_set = a_set.add(1)
print(a_set)

3. How to extract/inspect items in a set

When you use sets, you usually want to compare the elemets of different sets, for instance, to find out how much overlap there is or how many of the items in set1 are not members of set2. Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetric difference. Please take a look at this website if you prefer a more visual and more complete explanation. Here we explain the two most common methods (don't worry about the rest for now):


In [ ]:
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

The union method takes one positional argument (a set) and returns the union of two sets.


In [ ]:
help(set.union)

In [ ]:
set1.union(set2)

The intersection method takes one positional argument (a set) and returns a new set containing only the intersection of the two sets.


In [ ]:
help(set.intersection)

In [ ]:
set1.intersection(set2)

Since sets are unordered, you can not use an index to extract an element from a set.


In [ ]:
a_set = set()
a_set.add(1)
a_set.add(2)
a_set[0]

4. Using built-in functions on sets

The same range of functions that operate on lists also work with sets. We can easily get some simple calculations done with these functions:


In [ ]:
nums = {3, 41, 12, 9, 74, 15}
print(len(nums)) # number of items in a set
print(max(nums)) # highest value in a set
print(min(nums)) # lowest value in a set
print(sum(nums)) # sum of all values in a set

5. An overview of set operations

There are many more operations which we can perform on sets. Here is an overview of some of them. In order to get used to them, please call the help function on each of them (e.g. help(set.union)). This will give you the information about the positional argument, keyword arguments, and what is returned by the method.


In [ ]:
set_a = {1, 2, 3}
set_b = {4, 5, 6}
an_element = 4


print(set_a)

#do some operations 
set_a.add(an_element)  # Add an_element to set_a
print(set_a)
set_a.update(set_b)     # Add the elements of set_b to set_a
print(set_a)
set_a.pop()         # Remove and return an arbitrary set element. How does this compare to the list method pop?
print(set_a)
set_a.remove(an_element)     # Remove an_element from set_a
print(set_a)

You can use the dir built-in function to see an overview of all set methods:


In [ ]:
dir(set)

Exercises

Exercise 1:

Please create an empty set and use the add method to add four items to it: 'a', 'set', 'is', 'born'


In [ ]:

Exercise 2:

Please use a built-in method to count how many items your set has


In [ ]:

Exercise 3:

How would you remove one item from the set?


In [ ]:

Exercise 4:

Please check which items are in both sets:


In [ ]:
set_1 = {'just', 'some', 'words'}
set_2 = {'some', 'other', 'words'}
# your code here