1 00:00:00,480 --> 00:00:01,859 Instructor: Can you believe it? 2 00:00:01,859 --> 00:00:06,510 I hope I didn't put you to sleep, but we're almost there. 3 00:00:06,510 --> 00:00:11,510 We have one last main data type, set, and then we're done. 4 00:00:11,670 --> 00:00:15,090 I know, I know this isn't the most exciting topic. 5 00:00:15,090 --> 00:00:16,950 Trust me, this is the hardest part. 6 00:00:16,950 --> 00:00:17,970 When you're starting out 7 00:00:17,970 --> 00:00:19,710 and you're just learning these things 8 00:00:19,710 --> 00:00:22,110 without understanding why do I need this, 9 00:00:22,110 --> 00:00:23,760 how am I gonna build the next game, 10 00:00:23,760 --> 00:00:26,100 how am I gonna build the next Netflix, 11 00:00:26,100 --> 00:00:30,420 how am I gonna build the next coolest Apple app, 12 00:00:30,420 --> 00:00:31,867 you're looking at this and thinking, 13 00:00:31,867 --> 00:00:33,630 "I just learned a bunch of theory. 14 00:00:33,630 --> 00:00:36,000 I feel like I'm in math class all over again." 15 00:00:36,000 --> 00:00:37,590 I know, trust me, I went through this. 16 00:00:37,590 --> 00:00:39,600 I was a self-taught programmer as well. 17 00:00:39,600 --> 00:00:42,480 These are the Lego blocks that builds our foundation. 18 00:00:42,480 --> 00:00:45,570 Once we learn these, we can move on to new things. 19 00:00:45,570 --> 00:00:48,840 And you'll see that once you climb this little mountain, 20 00:00:48,840 --> 00:00:50,790 things become so much easier, 21 00:00:50,790 --> 00:00:52,500 and you'll realize the power 22 00:00:52,500 --> 00:00:54,480 when we start building these projects 23 00:00:54,480 --> 00:00:57,487 toward the end of the class where you're gonna say, 24 00:00:57,487 --> 00:00:59,520 "Oh, all these blocks that we learned, 25 00:00:59,520 --> 00:01:04,290 all this, frankly, boring part is all going to make sense." 26 00:01:04,290 --> 00:01:05,550 So hang in there. 27 00:01:05,550 --> 00:01:08,190 It's supposed to be hard, it's supposed to be tough, 28 00:01:08,190 --> 00:01:11,730 but you'll see very soon why this is so powerful 29 00:01:11,730 --> 00:01:14,040 and why what we've just learned is gonna allow us 30 00:01:14,040 --> 00:01:16,920 to build some really awesome things 31 00:01:16,920 --> 00:01:19,470 and create some really awesome programs. 32 00:01:19,470 --> 00:01:24,470 So let's finish off with the last one, the last data type, 33 00:01:24,840 --> 00:01:29,370 the last data structure that we'll see in Python, 34 00:01:29,370 --> 00:01:34,370 at least from the main ones, and it's called set. 35 00:01:34,650 --> 00:01:39,650 And sets are simply unordered collections of unique objects. 36 00:01:41,280 --> 00:01:42,810 Hmm. Let's have a look. 37 00:01:42,810 --> 00:01:44,417 Let's create a my_set, 38 00:01:46,169 --> 00:01:50,640 and my_set we create with curly brackets like a dictionary, 39 00:01:50,640 --> 00:01:54,090 but instead of a dictionary, we don't have key value pairs. 40 00:01:54,090 --> 00:01:59,090 Instead, we just have values like 1, 2, 3, 4, 5. 41 00:02:00,510 --> 00:02:02,250 That's it. That's a set. 42 00:02:02,250 --> 00:02:03,930 We just created a set. 43 00:02:03,930 --> 00:02:06,360 But remember the definition I gave you. 44 00:02:06,360 --> 00:02:11,360 Set is an unordered collection of unique objects. 45 00:02:11,850 --> 00:02:12,840 What does that mean? 46 00:02:12,840 --> 00:02:14,790 Well, if I print this 47 00:02:14,790 --> 00:02:19,790 and say my_set and I click run, I get a set. 48 00:02:20,520 --> 00:02:25,503 But if I had, let's say, another 5 in here and I click run, 49 00:02:26,940 --> 00:02:31,940 it only returns the unique sets or unique items. 50 00:02:32,880 --> 00:02:35,370 You see in a set, there's no duplicates. 51 00:02:35,370 --> 00:02:37,713 Everything has to be unique. 52 00:02:38,670 --> 00:02:42,420 So this 5 just never gets added to a set. 53 00:02:42,420 --> 00:02:46,500 For example, you can add things to a set, like my_set, 54 00:02:46,500 --> 00:02:50,313 and then I can say .add and give it a value, like 100. 55 00:02:51,240 --> 00:02:56,240 And then let's say I do my_set.add and then give it 2. 56 00:02:56,340 --> 00:03:01,290 If I click run here, I see that I was able to add 100, 57 00:03:01,290 --> 00:03:04,020 but 2, well, 2 wasn't really added 58 00:03:04,020 --> 00:03:08,700 because my_set already contained that data. 59 00:03:08,700 --> 00:03:10,740 It's unordered. 60 00:03:10,740 --> 00:03:13,680 I mean, we've created 1, 2, 3, 4, 5 here, 61 00:03:13,680 --> 00:03:16,590 but there's no real order to it. 62 00:03:16,590 --> 00:03:18,210 There's no bookshelf 63 00:03:18,210 --> 00:03:21,180 in our memory space that is right next to each other. 64 00:03:21,180 --> 00:03:23,280 These are all over the place in memory, 65 00:03:23,280 --> 00:03:28,280 but a set is able to find them because they're all unique. 66 00:03:28,290 --> 00:03:30,693 So they're all in just one location in memory. 67 00:03:31,590 --> 00:03:34,380 So let me ask you this, 68 00:03:34,380 --> 00:03:37,290 and this is gonna be a fun little exercise. 69 00:03:37,290 --> 00:03:39,723 If I gave you an array, 70 00:03:40,650 --> 00:03:44,643 and let's say this array contained, well, this right here, 71 00:03:45,780 --> 00:03:49,590 and I want you to return an array with no duplicates. 72 00:03:49,590 --> 00:03:54,370 So I want everything in, let's say, let's call it my_list, 73 00:03:57,720 --> 00:04:00,183 and a programmer has already created this. 74 00:04:01,110 --> 00:04:05,446 Okay, and then your task is to create 75 00:04:05,446 --> 00:04:10,446 or return a list or a collection that has only unique items. 76 00:04:14,100 --> 00:04:17,339 How would you go about converting this into a set? 77 00:04:17,339 --> 00:04:19,019 Well, we've seen this before. 78 00:04:19,019 --> 00:04:22,560 We have a set function, 79 00:04:22,560 --> 00:04:25,893 and we simply wrap my_list in that set, 80 00:04:27,150 --> 00:04:28,300 and let's print it out. 81 00:04:29,760 --> 00:04:32,790 I click run. Check it out. 82 00:04:32,790 --> 00:04:36,033 We have formed a new set from a list, 83 00:04:37,380 --> 00:04:40,200 and I've removed all duplicate values. 84 00:04:40,200 --> 00:04:42,060 When would this be useful? 85 00:04:42,060 --> 00:04:46,350 Imagine if we had usernames, right, or email addresses. 86 00:04:46,350 --> 00:04:49,290 We're collecting email addresses on our startup page, 87 00:04:49,290 --> 00:04:52,080 but we don't wanna have duplicate emails. 88 00:04:52,080 --> 00:04:56,130 We might wanna convert this, a list of emails, to a set 89 00:04:56,130 --> 00:04:57,360 and remove any duplicates 90 00:04:57,360 --> 00:04:59,700 so we're not sending emails over and over. 91 00:04:59,700 --> 00:05:01,053 So that's really cool. 92 00:05:01,890 --> 00:05:02,940 What about this? 93 00:05:02,940 --> 00:05:06,660 Can I, let's change this into a set. 94 00:05:06,660 --> 00:05:08,117 So let's call it my_set. 95 00:05:10,080 --> 00:05:12,663 Going to change this into a set. There you go. 96 00:05:13,800 --> 00:05:18,800 And then what if I wanted to access my_set at index of 0? 97 00:05:20,430 --> 00:05:23,490 Hmm. Set object does not support indexing. 98 00:05:23,490 --> 00:05:26,700 Well, again, you can think of it more as a dictionary. 99 00:05:26,700 --> 00:05:28,890 In order to access a set, 100 00:05:28,890 --> 00:05:33,750 we have to grab by the item that's in it. 101 00:05:33,750 --> 00:05:37,500 That's the key to getting the information from memory. 102 00:05:37,500 --> 00:05:38,970 The way we would check 103 00:05:38,970 --> 00:05:41,797 if something exists is we simply say, 104 00:05:41,797 --> 00:05:46,797 "Hey, is 1 in my_set?" 105 00:05:47,160 --> 00:05:51,750 And we run this, and we get True. 106 00:05:51,750 --> 00:05:56,750 We can also do length of my_set and we get 5. 107 00:05:58,590 --> 00:06:02,820 Again, nothing too crazy, but 1, 2, 3, 4, 5. 108 00:06:02,820 --> 00:06:05,310 Remember, it only counts the unique things 109 00:06:05,310 --> 00:06:07,350 because this will never gets entered. 110 00:06:07,350 --> 00:06:08,730 It's a set. 111 00:06:08,730 --> 00:06:12,510 And alternatively, we can also convert this into a list. 112 00:06:12,510 --> 00:06:14,970 If I click run here, 113 00:06:14,970 --> 00:06:18,693 you'll see that I now have my_set as a list. 114 00:06:19,710 --> 00:06:22,099 And then we can also do, let's say, 115 00:06:22,099 --> 00:06:27,099 my_set.copy and actually copy my_set into something, 116 00:06:28,770 --> 00:06:32,260 let's say, new or new_set. 117 00:06:33,150 --> 00:06:37,650 And then this will be completely different or a new copy 118 00:06:37,650 --> 00:06:39,840 then this original one. 119 00:06:39,840 --> 00:06:42,550 So let's test this out. This new_set. 120 00:06:44,970 --> 00:06:49,080 If I use a method that we may have seen before, 121 00:06:49,080 --> 00:06:51,930 which is my_set.clear, 122 00:06:51,930 --> 00:06:54,150 which, as you can imagine, clears the set, 123 00:06:54,150 --> 00:06:58,380 and I click run, I have that new set, 124 00:06:58,380 --> 00:07:03,380 but when I try and print our all set, which was my_set, 125 00:07:04,680 --> 00:07:08,790 it's going to be empty, an empty set. 126 00:07:08,790 --> 00:07:10,980 But the true power of sets 127 00:07:10,980 --> 00:07:13,680 comes in the next couple of methods 128 00:07:13,680 --> 00:07:15,180 that I'm going to show you. 129 00:07:15,180 --> 00:07:18,210 And sets have quite a few methods. 130 00:07:18,210 --> 00:07:22,080 But don't worry, most of them are quite similar. 131 00:07:22,080 --> 00:07:24,123 So let's explore that in the next video.