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: