Sets

Lists and tuples are ordered collections. Sets are a different type of collection. They are not ordered, and no duplicate items are allowed. Differences of collection types:

Collection Ordered? Mutable? Unique items?
Lists True True False
Tuples True False False
Sets False True True

Set Basics

Sets are unordered collections of unique elements. When we create sets, we use the curly braces to surround the list of elements.

>>> colors = {'red', 'blue', 'yellow', 'red'}
>>> colors
{'red', 'yellow', 'blue'}

We can see that 'red' only appears once in our set even though we added it twice. Note that there was no error for including a duplicate item. It is simply ignored.

Sets are mutable so we can add and remove elements from them:

>>> colors.remove('yellow')
>>> colors
{'red', 'blue'}
>>> colors.add('purple')
>>> colors
{'red', 'purple', 'blue'}

You can use sets to find all non-duplicate elements in a list by using the set constructor:

>>> names = ['Patricia', 'Bryan', 'Michelle', 'Bryan']
>>> unique_names = set(names)
>>> unique_names
{'Patricia', 'Michelle', 'Bryan'}

How do you make an empty set?

>>> s = set()
>>> s
set()
>>> s.add(4)
>>> s
{4}

We cannot make an empty set with empty curly braces because that makes a dictionary.

Because the sets are not ordered, you cannot address an item by its index, since it doesn’t have one.

>>> s[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing

Note that there is no guarantee to the order of a set:

>>> colors
{'blue', 'red', 'purple'}
>>> colors.add('green')
>>> colors
{'green', 'blue', 'red', 'purple'}

We can use type to verify that we’re working with a set:

>>> type(colors)
<class 'set'>

Help works on sets just like on strings, ints, and lists:

>>> help(unique_names)
>>> help(set)

Set Operations

Sets work like sets in math. A number of methods can be used to perform logic on multiple sets. There are also operators that can be used as shortcuts.

First let’s make some sets:

>>> a = {1, 2}
>>> b = {2, 3}

We can take the union of our sets

>>> a.union(b)
{1, 2, 3}
>>> a | b
{1, 2, 3}

We can take the intersection of our sets

>>> a.intersection(b)
{2}
>>> a & b
{2}

We can also find elements that are in one set but not both sets:

>>> a.symmetric_difference(b)
{1, 3}
>>> a ^ b
{1, 3}

We can remove elements that are in one set from another set:

>>> a.difference(b)
{1}
>>> a - b
{1}
>>> b.difference(a)
{3}
>>> b - a
{3}

Set Conditionals

We can check for containment in a set:

>>> 1 in a
True
>>> 3 in a
False

We can also check whether one set is a subset of another set:

>>> c = {2, 3, 4}
>>> a.issubset(c)
False
>>> b.issubset(c)
True
>>> b <= c
True
>>> a <= c
False

We can also check for supersets and other relations:

>>> c.issuperset(b)
True
>>> c.issuperset(a)
False
>>> c >= b
True
>>> c >= a
False
>>> a <= a
True
>>> a < a
False

Set Exercises

Where to Eat

Three of us want to eat lunch together but we can’t decide where to eat. Lets make three sets containing all of the restaurants we want to eat at and take the intersection of these sets to determine where to eat.

>>> restaurants_trey = {'Habaneros', 'Karl Strauss', 'Opera', 'Punjabi Tandoor'}
>>> restaurants_diane = {'Siam Nara', 'Punjabi Tandoor', 'Opera'}
>>> restaurants_peter = {'Karl Strauss', 'Opera', 'Habaneros'}

Count favorites

Use the three restaurant sets we used before to create a dictionary containing the number of times each restaurant occurs in a set:

>>> restaurants_trey = {'Habaneros', 'Karl Strauss', 'Opera', 'Punjabi Tandoor'}
>>> restaurants_diane = {'Siam Nara', 'Punjabi Tandoor', 'Opera'}
>>> restaurants_peter = {'Karl Strauss', 'Opera', 'Habaneros'}

So one of the entries in the dictionary would be 'Opera' : 3.

Bonus: Most common

Create a function that accepts a list of sets and returns a set of the most common items from each of the given sets.

For example:

>>> most_common([{1, 2}, {2, 3}, {3, 4}])
{2, 3}
>>> most_common([restaurants_trey, restaurants_diane, restaurants_peter])
{'Opera'}

Bonus: Check For Duplicates

Write a function is_unique that takes a list as input and returns True if the list contains no duplicate elements.

Try running is_unique on the following lists:

>>> numbers1 = [1, 2, 4, 2]
>>> numbers2 = [1, 2, 3, 4]