# Python language basics 52: basic set algebra operations

October 3, 2015 Leave a comment

**Introduction**

In the previous post we saw how to remove an object from a set in Python. We discussed the usage of two methods, namely ‘remove’ and ‘discard’ and how they differed.

In this post we’ll go through 4 useful functions from the world of set algebra.

**Set algebra**

You must have come across sets in your basic algebra studies at school. Remember how your teacher demonstrated things like ‘unions’ or ‘intersections’ using circles or ellipses? Here’s an image from Wikipedia showing a set intersection:

Set algebra is a large topic. You can read more about it on Wikipedia here and here. In this post we’ll go through 4 basic operations.

*Intersection*

An intersection ” is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements.”

That’s quite simple I guess. Python has the ‘intersection’ method that returns the intersection of two sets:

set_a = {4, 6, 3, 5, 9, 8, 7} set_b = {6, 5, 7, 1, 2} set_intersection = set_a.intersection(set_b)

The resulting set will have the following elements:

{5, 6, 7}

…which are the ones that figure in both sets.

*Union*

A set union is the collection of all unique elements of two sets. If an element figures in both sets it will appear only once in the union set:

set_a = {4, 6, 3, 5, 9, 8, 7} set_b = {6, 5, 7, 1, 2} set_union = set_a.union(set_b)

‘set_union’ will hold the following integers:

{1, 2, 3, 4, 5, 6, 7, 8, 9}

…which are the ones that are unique across the constituent sets, i.e. set_a and set_b.

*Complement*

A complement is a set of elements that only figure in one of the sets but not both. Complement sets can be calculated using the ‘difference’ function in Python:

set_a = {4, 6, 3, 5, 9, 8, 7} set_b = {6, 5, 7, 1, 2} set_a_difference = set_a.difference(set_b) set_b_difference = set_b.difference(set_a)

‘set_a_difference’ will contain the following objects:

{8, 9, 3, 4}

…which are the ones that exclusively appear in set_a but not set_b. Conversely, ‘set_b_difference’ will hold the values that are only found in set_b but not set_a, that is…

{1, 2}

*Symmetric difference*

A symmetric difference is the exact opposite of an intersection: “the set of elements which are in either of the sets and not in their intersection.”

Let’s see what the symmetric difference of the two integer sets gives us:

set_a = {4, 6, 3, 5, 9, 8, 7} set_b = {6, 5, 7, 1, 2} set_symm_difference = set_a.symmetric_difference(set_b)

The resulting set contains the following:

{1, 2, 3, 4, 8, 9}

1 and 2 figure in set_b but not set_a. 3, 4, 8, 9 on the other hand appear in set_a but not set_b. The other elements, i.e. 5, 6, 7 appear in both which make up the intersection of the two sets we saw above, they are therefore discarded. That’s symmetric difference for you.

In the next post we’ll look at some basic set operations from boolean set algebra.

Read all Python-related posts on this blog here.