Sets
- 01:58
How sets are used in Python, the difference between sets and lists, and learn how to create and manipulate sets, including removing duplicates and the significance of using curly braces for sets.
Downloads
No associated resources to download.
Transcript
Let's take a look at a new type of object called the set. A set is very similar to a list in that it helps you organize other objects, but the major two differences are that objects in a set are all unique and they are not in a specific order. Look at list one on the left where we have the letters A through E in alphabetical order, and there are two of each letter. In contrast set one on the right only has unique values, and the letters are not in a specific order. Let's take a look at what this looks like in Python.
We're going to create a list and a set with the same exact objects so that you can see when we print out set one, it's going to remove the duplicates and it's going to scramble the order. Let's go ahead and execute this code cell. So now list one and set one have been defined. When I print out list one, it's gonna look exactly like the objects that we put into it, but when I print out set one, the duplicates have been removed and the order of the objects has been scrambled. Notice that the set is contained in curly braces, whereas the list is contained in square brackets. And the trick I like to use, like I just mentioned, is the list. The leftmost square bracket looks kind of like a capital L, and I like to imagine that that curly brace is an S. So L for lists, S for set, the list is a square bracket. The set is the curly braces, and that's super important because those symbols that contain what you're putting into the list or what you're putting into the set, those symbols on the outside are the only clue that Python has to know, I should treat this object like a list, or I should treat this object like a set. It's critical that you use the right symbols on the outside.