**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

A set is an unordered collection of unique elements. Sets can be used like a Venn Diagram to compare and contrast the membership of elements in two or more collections.

#### What is a Set?

An unordered collection of unique elements

#### Properties of a set

- Order does not matter
- Contains no duplicate values

#### What is a Member?

An element of a set

### Sets vs Lists

##### Similarities

- Mutable
- Collect single elements

##### Differences

- Sets have no index
- Sets are not a sequence

### Sets vs Dictionaries

##### Similarities

- Unordered collection
- Very fast with "look-up" operations

##### Differences

- Sets have no key-value pairs
- Set "look-up" is a test for membership
- "True or False: does it exist in the set?"

[MUSIC]
0:00

Hi, my name is AJ, and
I'm an instructor at Treehouse.
0:09

My pronouns include he, and they.
0:14

In this workshop, you're gonna learn about
a new type of Python data structure,
0:17

the set.
0:22

What exactly is a set?
0:24

A set is a collection type
with two basic properties.
0:26

First, the order of
elements does not matter.
0:31

Second, all of the elements
have unique values.
0:35

In other words,
a set contains no duplicate values.
0:40

When an element is in a set,
we say that it is a member of that set.
0:45

Membership is an important
distinguishing feature of sets, and
0:51

we will be exploring this
concept throughout the workshop.
0:55

The concepts of sets and
1:00

membership come from a branch of
mathematics called set theory.
1:01

You don't have to be a math expert to
understand how to use these in Python.
1:06

We'll start by visualizing
set membership with diagrams.
1:11

On a diagram,
a set is represented by a circle.
1:17

Let's imagine a set of fruit, apples,
bananas, oranges, lemons, and limes.
1:21

So the order of the fruit does not matter.
1:29

In fact, if I change the order and
then talk about limes, oranges, apples,
1:32

lemons, and then bananas, I'm still
talking about the same set of fruit.
1:37

The set contains unique elements.
1:43

All of our fruits are distinctly
different from each other.
1:46

This also means that a set
cannot contain duplicate data.
1:50

If someone asks you to name five fruits,
1:54

you wouldn't say bananas five times,
one is enough.
1:57

Since bananas are already in the set,
it cannot be added again.
2:02

You may have already encountered
sets in the form of Venn diagrams.
2:07

A Venn diagram allows us
to visually compare and
2:12

contrast the elements that exist in two or
more sets.
2:15

It's made of two or
more overlapping circles.
2:19

And the overlapping area
are where the elements or
2:22

members that both sets have in common.
2:26

Coffee and tea are both drinks
that are made with hot water.
2:29

But coffee comes from roasting the seed
of the coffee plant, which we call beans.
2:35

And tea comes from curing
the leaves of the tea plant.
2:40

Both coffee and
tea can have sugar, milk or cream.
2:44

Most importantly,
both drinks have caffeine.
2:49

Venn diagrams can be used
with more than two sets.
2:54

Let's imagine sets of ingredients of
three foods from around the world.
2:58

Burrito, panini, and banh mi.
3:03

That's a lot of ingredients
to keep track of.
3:06

Reorganizing all of this data into a Venn
diagram will make the information easier
3:09

to understand.
3:14

Elements go into different circles
based on their membership.
3:16

And the intersections contain
the elements that the sets have in common
3:20

with each other.
3:25

And notice how the values are not
duplicated, we just need one of each.
3:27

Now we can make comparisons about these
foods to learn about what ingredients they
3:33

do and do not have in common.
3:38

Using a Venn diagram is a good way to
visually analyze the differences and
3:41

similarities between multiple
collections of data.
3:45

In Python, we can use sets so
3:50

that our programs can think in the same
way that we do when we use a Venn diagram.
3:51

A lot of programming involves
taking an input of raw data and
3:59

then processing it to
output useful information.
4:03

So it's important to understand the
different ways to structure data in order
4:06

to process it.
4:11

In Python, you've learned about how
to use lists and tuples to collect
4:13

sequences of data, and dictionaries
to collect data and key-value pairs.
4:18

Sets share characteristics of
both lists and dictionaries.
4:25

Like lists, sets are mutable
collections of single elements.
4:31

Mutable means that the data can
be changed after its creation.
4:36

Unlike lists, however,
4:40

sets have no index because
they're not a sequence.
4:43

That makes sets more
similar to dictionaries,
4:49

which are also unordered collections.
4:52

In Python, both sets and dictionaries are
highly optimized for look-up operations.
4:56

In a dictionary, you can look up a key to
get a value like finding somebody's name
5:03

and then getting their phone number
when you look in an address book.
5:07

But sets do not contain key-value pairs.
5:12

Instead with sets,
we look up an element to check if it does,
5:15

or does not exist in the set.
5:20

This concept is called membership testing.
5:22

And we'll learn more about
how to do this later.
5:25

Many programming languages have an
implementation of sets to make it easy to
5:28

compare and contrast collections.
5:32

And they are all based in
the mathematical field of set theory.
5:34

But don't worry about the math.
5:38

I've got real-world examples to
explain these concepts in Python.
5:40

In the next video,
we'll start by making our first sets.
5:44

And by the end of this workshop,
5:49

you will be a member of the set of
Pythonistas who understand sets.
5:51

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

Sign up