Create Unique Unordered Collections in Python with Set

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet

A set is an unordered collection with no duplicate items in Python. In this lesson, you will learn how to create them, and perform basic operations to determine members in the set and compare the values from different sets.

I can create a set in Python by giving it the variable name, starting with a curly bracket and then starting to add my set items to it. I can add a list of animals here including monkey, bear, dog, and then I'm going to add monkey again. We'll add a cat to it, we'll add another bear, and then we'll add a gorilla. Now if I print this out for you, you can see that it eliminated the duplicates, so that's the key to a set. It doesn't allow duplicate values which makes it good for membership testing.

For membership testing, in order to see which values are in it we can say monkey in animals, it will return true. This works very quick, even for large sets. If we ask for something that's not in there, it'll return false. Then here's the tricky part. If you want to create an empty set, whenever I created the one for animals above I used the curly braces. If I want to create an empty set, I have to use the word set with parentheses after it.

To add something to it, we have an add method, and I can add a shark to it. I can add a guppy to it, and I can add a whale to it. Then when we take a look at it, it prints out. Then I can remove items using the remove method, now I'll remove whale because it's not actually a fish. Sets also have a union method which allows you to merge two different sets together. I can merge fish with animals and it returns a new set that contains the de-duplicated values from both of those lists.