Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Sets in Python (Summary)

This lesson is from the Real Python video course by James Uejio.

00:00 This is the summary video for the sets course. Let’s go through what you learned. In Section 1, you were introduced to sets and learned what is a set and immutable versus hashable.

00:12 Remember that immutable objects are objects that cannot be modified after they were created, and hashable objects are any type of object that you can call the hash() method on. All immutable objects are hashable, but not all hashable objects are immutable. Python sets can only include hashable objects.

00:31 You also learned how to define a set in Python. In Section 2, you learned different ways to operate on a set. You learned how to get the length of a set, check membership, and iterate through a set, which were all the same as how you would check length, membership, and iterate through any other iterable such as lists and tuples. You learned some nice built-in methods to find the union set, which is the set of all elements in all sets; the intersection set, which would be only elements that are in all sets; and the difference set, which are all elements that are in the first set and not in any of the rest.

01:08 You also learn how to check if two sets are disjoint, which means they do not have any elements in common. And you learned how to check if a set is a subset of another set, which means every element of the first set is in the second. And proper subset, which is the same as subset except sets can’t be identical.

01:25 You also learned superset, which is if the first set contains every element of the second set, and proper superset, which is the same as superset, but sets cannot be identical.

01:38 In Section 3, you learned different ways to modify a set. You learned about add, remove, discard, pop, and clear. And you learned different update methods, such as update, intersection update, difference update, and symmetric difference update.

01:52 These are all the same as their corresponding operators—update, intersection, difference, symmetric difference—but they actually mutate the set instead of returning a new set.

02:03 You learned that augmented assignment is not the same as the expanded out assignment for sets. So if you were to do an intersection update, remember that this always modifies a set.

02:14 It never creates a new set and reassigns it.

02:19 In Section 4, you learned about frozen sets, which are a special type of set because they are immutable. You learned about augmented assignment with frozen sets, which work differently than normal sets because augmented assignment on frozen sets is the same as the expanded out assignment.

02:36 This means that augmented assignment creates a new frozen set instead of mutating the original frozen set because, also, frozen sets are immutable. You also went through “Why sets?” and went through a speed test comparing sets with lists and tuples. You saw that checking membership in a set is much faster than checking membership in a list and tuple, and is one of the main reasons why we use sets in the real world.

03:04 Thank you so much for watching, and if you have any questions or comments, please leave them down below.

You must own this product to join the conversation.