17.11 Supersets and Subsets

Supersets and Subsets

A superset is a set that contains all elements that are in a given set. For example, here a is a superset of b:

a = {0, 1, 2, 3, 4}
b = {0, 3}
print(a.issuperset(b))  # prints True

A subset, on the other hand, is a set that is part of a given set. In the previous example, b is a subset of a:

a = {0, 1, 2, 3, 4}
b = {0, 3}
print(b.issubset(a))  # prints True

View on GitHub

Practice

Only Favorite Movies

Charles is going to the movie today and wants to figure out if all the movies played today are his favorite movies. Given a set containing the movies that are going to be played today and a set containing all his favorite movies, return True if all the movies played today are his favorite. Return False otherwise.

Previous Section

Next Section

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.