Transcript from the "Combining, Comparing, & Contrasting" Lesson
[00:00:00]
>> Nina Zakharenko: We can use operations to compare items between two sets, and there's just a cheat sheet here for some of them. There's more than this, but these are the ones that I generally find useful. So union or pipe symbol creates a new set with all the items from both s and t, and let's see that in action.
[00:00:33]
>> Nina Zakharenko: And very quickly just like list, if I wanna find out if an item is in my set or not, I can use the In keyword. So if I have a set of colors,
>> Nina Zakharenko: I can say is blue in colors? It is. Is orange in colors? It's not.
[00:01:02] There is some other methods on list that help us figure out if items were in there or not. One of those was .index. Do you think set has a .index? No, because the items in the set, they aren't positional. Just think about it like Santa's bag of presents, right?
[00:01:25] He's got presents in there, kind of all jumbled up. Doesn't really matter, right?
>> Nina Zakharenko: For a list, we also used a count method, right? Like to find out how many times a particular item appeared in the list. Does sets have a count method? No, right? Because each item is only in there once.
[00:01:50] Once it's in there that's it, if I try to add it to the set again it's not gonna get updated. Now set operations are really useful. So I'm gonna make two sets of colors and I recommend that you make your own sets. So the first one is gonna be the rainbow colors, and those are the same for everyone; red, orange, yellow, green, blue, and violet.
[00:02:27] And now make a new set of your favorite colors. Based on my hair, my favorite color is obviously pink, right? Pink hair, pink laptop, but I also like color black, I like the color blue.
>> Nina Zakharenko: So now you should have two sets. Just for clarity I'm gonna rename my favorite set favorite colors just so you can kind of follow along with me.
[00:02:59] To do that I can just assign a new variable to that one. Looking at my rainbow colors, red, orange, they're all in there. My favorite colors, black, blue, and pink.
>> Nina Zakharenko: Now.
>> Nina Zakharenko: We can use the union operator to create a new set that contains all of the items from both rainbow colors and favorite colors.
[00:03:40]
>> Nina Zakharenko: Remember you can use my set.union, other set,
>> Nina Zakharenko: Or you can use my_set, and this pipe symbol that means union.
>> Nina Zakharenko: Other set. Okay, so let's try this with rainbow colors and favorite colors.
>> Nina Zakharenko: And so all the items from rainbow colors and all the items from favorite colors.
[00:04:14] And we'll see that the set is a little bit shorter, right? All the items combined are shorter than the sets with the two different elements in them. Why is that?
>> Speaker 2: Remove the duplicates.
>> Nina Zakharenko: Right, remove the duplicates
>> Nina Zakharenko: Okay, next is the intersection.
>> Nina Zakharenko: So that's creating a new set with only the items that appear in both sets.
[00:04:44] And the symbol for intersection is the ampersand. So, rainbow rainbow_colors & favorite_colors. For me, that's just blue. Depending on what your favorite colors are, you might see more results or you might see less. Or you might get back nothing.
>> Nina Zakharenko: Now, lastly we're also gonna find the difference.
[00:05:10] The difference is, it's creating a new set with all the items that are in one, but not in the other side. So to do that we use this hat character. That's the difference.
>> Nina Zakharenko: And we'll see that blue Is missing from this list, right? Blue was in both and now it's gone, that's the intersection.
[00:05:42]
>> Nina Zakharenko: There are a lot of other useful operations available on set. You can see if one set is a subset of another set, or superset, there's a lot more. I don't have time to cover them all. Python also has a frozen set type available. If you need the functionality of a set, but you might want it to be what?
[00:06:06]
>> Speaker 3: Immutable.
>> Nina Zakharenko: Gold star, exactly right. Did everyone hear? Yeah, if you want your set to be a immutable, you can use a frozen set.
>> Nina Zakharenko: And don't forget, you can always call help on set, or dim, do a little bot of exploring. So if I wanted to call help on set.union, I would just have to pass in that method, and it'll kinda give me a helpful hint that union returns the union of sets as a new set.
[00:06:42] Actually, that's not a good explanation, right? It's like the definition is the definition.
>> Speaker 3: [LAUGH]