- [Tutor] Up to this point in the videos,
we've been focusing on a number
of different built in collections in Python.
We've looked in detail now at lists, tuples,
and in this lesson we've been focusing on dictionaries.
Next up, we'd like to take a look at the set collection,
which is also built into Python.
A set is a collection of unique values,
an unordered collection of unique values specifically,
so you should never depend on the order
in which items appear in a set.
It is mainly used to determine membership,
whether or not something is in a set
and we're going to show you a number of different operations
that are used along those lines.
So let's go ahead and create our very first set.
In this case, we have a set of strings called colors.
And you'll notice that sets are delimited by curly braces,
just like dictionaries.
However, in a dictionary we have a key, a colon and a value
for each item in the comma separated list.
Whereas in a set, we just have a key in each item
that we are passing into the curly braces here.
So another thing I wanna point out
is that I don't have all unique values here.
So what happens is, as the set gets created,
each of these values is going to be inserted into the set
and then when we get to the second copy of red,
it will try to insert that into the set,
see that the value is already there
and simply discard the extra value.
So duplicate elimination is one of the key capabilities
of sets it ignores the duplicates.
So let's go ahead and evaluate colors
and you'll notice two things about
the resulting curly bracketed list of keys.
In this case, there's only one key
with the value red even though we started with two
in the statement that we wrote up above.
And you'll also notice that the values are not
in the same order that we specified
when we created the set in the first place.
So you should never rely on the order
in which the elements are listed inside of a set.
It is mainly again used for membership style testing.
Now you can determine how many elements,
are in a set very easily.
So we have that built in len function once again
and if you pass it a set,
it will count up the number of elements in that set
and give that back to you.
And you can also very easily check whether a value,
is in a set by using the in and not in operators.
So if you want to do a membership test, for example,
if you wanna see if red is in colors,
you can write an expression like this,
you might use that in an if statement for example,
and in this case, we can see that indeed,
red is in colors whereas if we do purple in colors,
of course, that's false because purple is not in colors.
And along those same lines,
if I then test not in colors, purple not in colors,
you can see that indeed, that's true.
Now, as you might expect,
you are able to iterate through sets,
just like you can iterate through lists,
and tuples, and dictionaries and strings.
So, again, Python's really flexible
in that you can do a lot of the same things
across many different data types.
So sets are iterable
and here's a little for loop,
that's going to walk through each color
in the set called colors and for each one,
in this case, we're going to give you
the all uppercase version of the string,
separated from the next by a space.
So let's go ahead and do that
and you can see we get the same values once again.
However, I would like you to notice a couple of things here.
Number one, they're all uppercase,
because we called upper on each element.
And number two, they're not in the same order
that they were displayed up above either.
So again, don't rely on the order
of the elements within that set,
I can't emphasize that enough.
Now, you can also create sets,
using a built in function called set.
So to demonstrate this concept,
let me first create a list called numbers.
And in this particular case, we have a list
with the elements from zero to nine in it
that we're going to create,
and we're going to concatenate that
with another list containing zero through four.
So there will be some duplicates in this list.
And to show you that we'll display the list here.
So you see zero through nine and then zero through four.
So zero through four are duplicated twice
in this particular list.
Now, when you use the set built in function,
and you give it a sequence of values,
it's going to create a set object.
And as you might expect,
that set object will have only the unique items
from the argument sequence.
So if I do that, you can see we wind up
with a set containing only zero through nine.
And the duplicate values at the end of the list
were simply ignored as the set was created.
Now, you may recall that when I created an empty dictionary,
I did that with an empty set of curly braces.
Well, that means I can't use an empty set of curly braces
to define an empty set object.
So if you do need an empty set,
the way you're going to do that is with the set function
with no arguments,
and that is going to be represented in its string format,
as the word set followed by empty parentheses.
So sets are a little bit different
in terms of how they are represented in string format
for an empty set versus an empty list
which would be displayed as square brackets,
an empty tuple, which would be displayed as parentheses,
or an empty dictionary,
which would be displayed as a set of curly braces.
Now, just for your information sets are mutable,
meaning you can modify their contents.
The elements within sets, however, have to be immutable,
because they have to also be hashable,
only immutable objects can be hashable.
And the hashability is what enables items to be placed
into a set or a dictionary and allows the data structures
to maintain those unique keys.
Now, it turns out that there is a type
in the Python standard library called frozen set,
which is an immutable representation of a set.
So if in fact you need a frozen set of elements,
meaning that once the set is created,
it can never be modified.
Then you can use the frozen set type
and frozen sets because they are immutable,
if they also contain only immutable objects
which they have to for keys,
you can then use them actually as keys in other sets,
or in dictionaries as well.