**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Preview

Start a free Courses trial

to watch this video

There are four main set operations that allow you to compare and contrast collections like a Venn Diagram. Union and Intersection are two methods for comparing shared membership between sets.

#### Main Set Operations

The following methods and operations return a new set.

`s.intersection(t)`

/ `s & t`

- Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.)

`s.union(t)`

/ `s | t`

- Return the union of sets as a new set. (i.e. all elements that are in either set.)

In our last video,
we used Euler diagrams to illustrate
0:00

the relationship between a subset and
a superset.
0:04

A set can be a subset of another set when
all of its members are found in another.
0:08

What happens when some, but not all of
the members of one set are in another?
0:15

We can illustrate these
relationships with a Venn diagram.
0:21

Remember that we can use
a Venn diagram to compare and
0:26

contrast the memberships of sets.
0:30

The common members go where
the circles overlap, and
0:33

unique members stay on the sides.
0:38

Each section of the Venn diagram
is a set with a specific name
0:41

that describes a subset or
superset relationship.
0:46

Let's use real-world examples to learn
about these special Venn diagrams sets and
0:50

what they are called.
0:56

I used to work in restaurants, and
0:57

my favorite time to work
was the breakfast service.
1:00

Let's imagine comparing two omelets.
1:03

We can represent the ingredients of
each omelet as members of a set.
1:06

The entire diagram can be thought of
as a superset, we call that the union.
1:11

Union is a set of all of
the members of both sets.
1:17

Intersection is a subset of all of
the members common to both sets.
1:22

It is where the Venn diagram overlaps.
1:28

This can be an empty set.
1:31

Difference is a subset of the members
unique to one set, but not the other.
1:33

In Python, it will be the leftmost set.
1:39

Symmetric difference is a subset
of the members unique to each set.
1:43

So members of the left and
right sides but not the middle.
1:48

It's equivalent to the union
minus the intersection.
1:52

Now let's try it out in Python.
1:58

Our restaurant has been doing well and
we now have five omelets on our menu.
2:00

Just like in a real restaurant's menu, the
ingredients are in no particular order.
2:06

We need to learn about
the similarities and
2:11

differences between the omelets we serve.
2:14

First, I need to know about all
of the ingredients to stock so
2:16

that my kitchen doesn't run out of food.
2:20

We can take the union of our five sets
to get a set of all the ingredients that
2:23

I need in my kitchen.
2:27

There are three ways to do this.
2:28

And we'll start small,
first with the union of two omelets.
2:31

I'll take the union of the turkey and
vegetarian omelets first.
2:38

turkey.union(vegetarian).
2:42

And we'll print this out to the terminal.
2:48

We get back a set of all the ingredients
with the duplicates removed.
2:56

And we can chain the method call with more
dot notation to get the union of more
3:00

than two omelets.
3:05

Here, I'll add in a .union, and
I'll take the union of turkey,
3:08

vegetarian, and the denver omelets.
3:14

Chaining method calls together with
dot notation can become very verbose.
3:20

You can imagine how far this
line of code on line 12 will
3:25

stretch if I pass in all five
omelets in the same manner.
3:29

Luckily for us, the union method can
also take more than one argument
3:34

All I have to do is separate
them by commas, and
3:41

when I print in my terminal,
I get the exact same thing,
3:45

a set of all the ingredients
with the duplicates removed.
3:49

For five sets, this process
can still become very verbose.
3:54

So we can also use a shorthand
operator for union,
3:58

which is the vertical pipe symbol.
4:01

On the keyboard, that's between
the Backspace and Return keys or
4:04

Shift and Backslash.
4:09

We're making a new set.
4:11

So first, I'll declare a new variable,
all_ingredients.
4:12

And assign it to the union
of all five omelets,
4:18

turkey | denver | vegetarian
| bacon | smothered.
4:24

And I'll print this out to the terminal.
4:32

There we have it.
4:48

A nice and concise way of printing
out all of the ingredients
4:49

with none of the duplicates.
4:54

This is the union.
4:56

Great, next I need to
know what ingredients
4:59

are common to all five omelets.
5:03

They will be the most important,
5:06

because if I run out of any of these
ingredients, I can't make any more food.
5:08

In other words,
5:13

I need to find the intersection of all
of the ingredients of all five omelets.
5:14

The intersection is the middle
overlapping part of the Venn diagram.
5:20

Let's start with two, the vegetarian and
bacon omelets this time.
5:26

vegetarian.intersection(bacon).
5:32

We'll print this out in our terminal.
5:40

The ingredients in common
of the vegetarian and
5:50

bacon omelets are onion, cheese,
eggs, mushroom and avocado.
5:53

And for five, we can use the shorthand
operator, which is the ampersand symbol.
5:58

shared_ingredients =
6:06

vegetarian & bacon &
6:12

denver & mothered & turkey.
6:17

print(shared_ingredients)
6:28

Eggs, onion, and
cheese are the common ingredients
6:41

shared between all five of my omelette.
6:45

So in my restaurant, I'll make sure to
stock up extra of eggs, onion, and cheese.
6:49

You need to sign up for Treehouse in order to download course files.

Sign up