Set and set operations in Python

on , 3 minutes reading

Last week I had a phone chat with my mom, she was a school teacher for many years and we ended up talking about basic things you learn in school, for example, sets. Yes, as many other students, I learnt sets in primary school but it was the typical simple explanation using Venn diagrams and how you do basic set operations. Recently in my classes I learn more about sets, and how important they are for the specification of type in languages, and well, set theory can be very deep and fun as well.

Many common programming languages have a library type or modules for the idea of a set, for example, in Elixir we have the MapSet structure to represent a set and its operations, even JavaScript has the concept of set in its standard (though not operations are included). In Python, since Python 2.3, sets are a type included in the standard library and for a common multipurpose language I am surprised about the support for set operations included with the set object.

To create a set in Python, for example, the set \(A = \{1,2,3\}\) we use the operation set, we need to pass a list of elements. Remember, even when we pass repeated elements a set has unique elements!

A = set([1, 2, 3, 1])
Out[*]:
{1, 2, 3}

We can join two sets using the union operation, for example: \(A \cup \{4, 5, 6\} = \{1, 2, 3, 4, 5, 6\}\):

A.union(set([4, 5, 6]))
Out[*]:
{1, 2, 3, 4, 5, 6}

We can use the operator | to express the same:

A.union(set([4, 5, 6])) == A | B
Out[*]:
True

What about intersection? Well, again, Python has us covered: \(A \cap \{3, 4, 5\}\):

A.intersection(set([3, 4, 5]))
Out[*]:
{3}

Another way of specify the intersection of two sets in Python is using the operator &

A.intersection(set([3, 4, 5])) == A & B
Out[*]:
True

How do we know if an element is in the set? How do we express \(3 \in A\)? Easy! remember the operator in?

3 in A, 5 in A
Out[*]:
(True, False)

Of course we can do the difference of sets as well, \(A - \{3, 4, 5\} = \{1, 2\}\):

A - set([3, 4, 5])
Out[*]:
{1, 2}

I was surprised Python includes symmetric difference as part of the set object, \(A \oplus \{3,4,5\} = \{1,2,4,5\}\):

A.symmetric_difference(set([3, 4, 5]))
Out[*]:
{1, 2, 4, 5}

Or the equivalent using the ^ operator

A.symmetric_difference(set([3, 4, 5])) == A ^ B
Out[*]:
True

Additionally to this we have a few other additional operations:

  • The cardinality of a set (\(|A|\)) using len(A)
  • Is B a subset of A? \(\{2, 3\} \subset A\) can be expressed as A.issubset(set([2, 3]))
  • Is B a superset of A? \(\{1, 2, 3, 4, 5\} \supset A\) is just simply set([1, 2, 3, 4, 5]).issuperset(A)

And of course, methods to add and remove elements from the set.

There is a lot of information about sets in Python in Python Course, go and take a look if you are curious :)