Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Set basics
Relationships between sets
14. The issubset() and issuperset() functions
Summary

Instruction

Finally, let's look at two functions that return either True or False: issubset() and issuperset(). First of all, let's define subsets and supersets.

Set A is a subset of a set B if each element of the set A belongs to set B as well. (The empty set is a subset of every other set, but we won't bother you with such details).

Set A is a superset of a set B if set A contains all elements of set B (but it may also contain other elements).

We can use issubset() to find out if all Kate's movies are also included in Alan's favorites:

kate_fav_movies = {'Halloween', 'A Star Is Born', 'Hocus Pocus'}
alan_fav_movies = {'A Star Is Born', 'Halloween', 'Harry Potter', 'Hocus Pocus', 'The Lion King'}
kate_fav_movies.issubset(alan_fav_movies)

This returns True because this list of Alan's favorite movies contains all of Kate's favorite movies. So, is the reverse true? Are Alan's movies the superset for Kate's? Let's find out:

kate_fav_movies = {'Halloween', 'A Star Is Born', 'Hocus Pocus'}
alan_fav_movies = {'A Star Is Born', 'Halloween', 'Harry Potter', 'Hocus Pocus', 'The Lion King'}
alan_fav_movies.issuperset(kate_fav_movies)

Yes, we can say that they are. Take a look at the diagram:

Exercise

You are given two sets related to XYC Corp. employees: all employees from Germany and all software developers. Write an if ... else ... statement and answer the following question: Do all XYC Corp. software developers work in Germany?

Print one of the two following sentences:

All software developers work in Germany.
Not all software developers work in Germany.

Stuck? Here's a hint!

Check if software_developers is a subset of employees_germany.