Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Disjoint, Subset, and Superset

00:00 Now, moving on to disjoint. Disjoint sets are sets that do not share any elements. Is disjoint checks whether or not two sets have any elements in common.

00:12 Here’s the method, x1.isdisjoint(x2). It only works with one argument because you’re only comparing two sets. And the argument needs to be a iterable. There is no corresponding operator.

00:24 Here is our example. We have x1 is a set with {'foo', 'bar', 'baz'}. x2 has {'baz', 'qux', 'quux'}. We check if they’re disjoint—they’re not disjoint, because they do share an element 'baz'. Let’s evaluate the difference of x2 and the set {'baz'}. That will give us {'quux', 'qux'}.

00:44 And if we check x1.isdisjoint(x2 - {'baz'}), that is True because x2 - {'baz'} is {'qux', 'quux'}, and that does not share any elements with x1.

00:58 Here’s another example. x1 = {1, 3, 5}, x2 = {2, 4, 6}. They are disjoint. And if we take the intersection, that will always be the empty set, because by definition if they do not share any elements, they are disjoint and the intersection is the empty set.

01:18 Let’s move on to subset. A set is considered a subset of another set if every element of the first set is in the second. Here we have the method x1.issubset(x2).

01:29 The argument needs to be iterable, and it only works with one set. And then here we have the operator, less than or equal (<=), which works with multiple sets. The operands need to be sets.

01:40 And it compares if each set is a subset of all of the rest of the sets to the right. Let’s look at some examples.

01:49 Here we have x is a set with {'foo', 'bar'}. We check, is that a subset of {'foo', 'bar', 'baz'}? It’s True.

01:58 And we check, is it a subset of itself? Well, yes. Technically, x contains all elements of x.

02:08 Here we have {1}, {1, 2}, {1, 2, 3}, and {1, 2, 4}. We check, is a a subset of b? Yes.

02:17 Is b a subset of c? Yes. Is a a subset of b a subset of c? Yes. Is a a subset of b a subset of d? Yes.

02:29 But then is a subset of c a subset of d? No, because c has to be a subset of d.

02:39 So, a proper subset is the same as subset except sets cannot be identical. There’s no method, and the operator is the less than symbol (<), which makes sense because subset is <=, and proper subset is <. The operands need to be sets, and it compares if each set is a proper subset of all the rest of the sets to the right.

03:02 So x1 < x1 would return False, while x1 <= x1 would return True. And that’s because x1 is not a proper subset of itself and x1 is a subset of itself.

03:19 I’m not going to go through any examples. You can look at the subset slides and just replace the <= with the <, and that would calculate proper subset.

03:29 The only time that it would be different is if you’re comparing a set with itself. Here we have superset. A set is considered a superset of another set if the first set contains every element of the second set. x1.issuperset(x2)that’s the method.

03:46 And the argument needs to be an iterable and only works with one argument. Here’s the operator, greater than or equal to (>=). It works with multiple sets and the operands need to be sets.

03:58 It compares if each set is a superset of all of the rest of the sets to the right. Let’s look at some examples.

04:06 We have a set {'foo', 'bar', 'baz'}. We check, is x a superset of {'foo', 'bar'}? That is True. Then we check if it’s a superset of itself. That is also True.

04:19 Now we have a set of {1}, a set of {1, 2}, {1, 2, 3}, and {1, 2, 4}. We check, is b a superset of a? That is True.

04:29 Is c a superset of b? That is True. Is c a superset of b a superset of a? That is True. And here we check that c is a superset of b, c is a superset of a, and b is a superset of a.

04:44 We’ll do the same thing here. This is True. Here we have d is a superset of c is a superset of a. That is False because d is not a superset of c.

04:59 So, a proper superset is the same as superset except sets can’t be identical. There is no method, and we use the greater than symbol (>).

05:08 The operands need to be sets, and this works with multiple sets and compares if each set is a proper superset of all the rest of the sets to the right.

05:17 x1 > x1 would return False, while x1 >= x1 would return True. Again, I’m not going to go through any examples—you can refer to the superset slides.

05:29 So now that you know how to operate on a set, in the next section, you’ll learn different ways to modify a set.

jwiede on Feb. 29, 2020

Might want to mention how empty set (set()) is a subset of all sets, and a proper subset of all sets (except itself, obviously).

jwiede on Feb. 29, 2020

(sorry, hit button too soon)

This can lead to unexpected results when a set you believe contains elements is actually empty, esp. if your code expects the set in question to NOT be a subset/proper subset of another set.

Assert the set in question is a proper superset of set() in order to catch such cases. If the set in question winds up empty, that assertion will fail.

Levi on March 14, 2020

Really good point jwiede, thanks for the info.

Become a Member to join the conversation.