1 00:00:00,870 --> 00:00:02,009 Instructor: Welcome back. 2 00:00:02,009 --> 00:00:03,840 Let's do an exercise where we check 3 00:00:03,840 --> 00:00:06,360 for duplicates in a list. 4 00:00:06,360 --> 00:00:09,030 So I have some_list here, 5 00:00:09,030 --> 00:00:13,980 and I want you to be able to check this list 6 00:00:13,980 --> 00:00:18,720 and print whatever the duplicate values are. 7 00:00:18,720 --> 00:00:21,870 In this case, we see that 'n' has duplicates 8 00:00:21,870 --> 00:00:24,780 and also 'b' has duplicates. 9 00:00:24,780 --> 00:00:29,780 So I want you to print 'b' and 'd' as a list in here. 10 00:00:31,050 --> 00:00:32,750 How would you go about doing that? 11 00:00:33,690 --> 00:00:35,880 Pause the video, give it a go, 12 00:00:35,880 --> 00:00:38,730 otherwise, I'm gonna keep going with the answer. 13 00:00:38,730 --> 00:00:43,730 And by the way, you're not allowed to use sets for this. 14 00:00:43,980 --> 00:00:48,510 So for this exercise, you can't use the data structure sets. 15 00:00:48,510 --> 00:00:51,600 Instead, you have to just use conditionals and loops 16 00:00:51,600 --> 00:00:54,000 that we've learned in the past couple of videos. 17 00:00:54,840 --> 00:00:58,740 So give it a try, pause the video, and when you're ready, 18 00:00:58,740 --> 00:01:00,490 I'm gonna go ahead with the answer. 19 00:01:02,010 --> 00:01:05,670 First, I'm going to create a variable called duplicates, 20 00:01:05,670 --> 00:01:07,530 and this is going to be an empty list 21 00:01:07,530 --> 00:01:11,190 that we're gonna populate with any duplicate values. 22 00:01:11,190 --> 00:01:13,710 Next, I'm going to loop over this list, 23 00:01:13,710 --> 00:01:18,710 so I'm going to say for value in some_list. 24 00:01:18,960 --> 00:01:23,310 And in here, well, what can we do? 25 00:01:23,310 --> 00:01:26,220 This is a tough one, but what I'm going to do 26 00:01:26,220 --> 00:01:31,220 is say if some_list.count. 27 00:01:32,520 --> 00:01:35,310 Remember, count allows us to count 28 00:01:35,310 --> 00:01:39,060 how many times an item in a list exists. 29 00:01:39,060 --> 00:01:44,060 And I'm going to say value so that some_list.count, 30 00:01:44,310 --> 00:01:46,620 and I give it 'a', it's going to count for me 31 00:01:46,620 --> 00:01:50,220 how many times 'a' happens in the list. 32 00:01:50,220 --> 00:01:51,960 Very, very useful. 33 00:01:51,960 --> 00:01:53,490 Again, if you didn't come up with that, 34 00:01:53,490 --> 00:01:55,170 there's many solutions to this problem. 35 00:01:55,170 --> 00:01:57,300 So there's no one right answer. 36 00:01:57,300 --> 00:02:00,180 I'm just showing you how I would've done it. 37 00:02:00,180 --> 00:02:03,150 Now here, I'm going to say if that's greater than 1. 38 00:02:03,150 --> 00:02:05,880 In that case, that means there's a duplicate. 39 00:02:05,880 --> 00:02:09,960 So I'm going to add it inside of the duplicates. 40 00:02:09,960 --> 00:02:13,800 So let's just go ahead and say .append 41 00:02:13,800 --> 00:02:17,523 because I wanna append, and in here say value. 42 00:02:19,050 --> 00:02:22,953 And at the end of all this, let's just print duplicates. 43 00:02:25,080 --> 00:02:26,970 Now, let's see if this works. 44 00:02:26,970 --> 00:02:30,780 I'm going to click run, and, all right, 45 00:02:30,780 --> 00:02:35,780 I get both 'b' and 'n', but I don't need this duplicates. 46 00:02:38,070 --> 00:02:40,530 I just need to know what are the duplicates. 47 00:02:40,530 --> 00:02:44,940 I just need one instance of 'b' and one instance of 'n'. 48 00:02:44,940 --> 00:02:48,120 So what we can do here is do another if statement 49 00:02:48,120 --> 00:02:53,120 and say if value is not in duplicates. 50 00:02:58,740 --> 00:03:00,810 And, yes, this is a bit of a trick. 51 00:03:00,810 --> 00:03:05,810 You may remember that not is the opposite, right. 52 00:03:05,910 --> 00:03:09,810 So we're checking, "Hey, is value in duplicates?" 53 00:03:09,810 --> 00:03:12,870 And not essentially says, "Hey, if value 54 00:03:12,870 --> 00:03:15,447 is not in duplicates it reads like English." 55 00:03:16,410 --> 00:03:18,963 And this is going to evaluate to true and false. 56 00:03:20,400 --> 00:03:21,450 And then, finally, 57 00:03:21,450 --> 00:03:23,310 make sure that we get the indentation right, 58 00:03:23,310 --> 00:03:25,860 we're going to append the value. 59 00:03:25,860 --> 00:03:30,860 So if I click run, I get 'b' and 'n'. 60 00:03:33,000 --> 00:03:36,840 Again, if you didn't get this right away, don't worry. 61 00:03:36,840 --> 00:03:39,030 It can get tricky, and this is one of those things 62 00:03:39,030 --> 00:03:41,130 that you just need to keep practicing. 63 00:03:41,130 --> 00:03:43,980 But you see here that I was able to use loops. 64 00:03:43,980 --> 00:03:48,300 I was able to use logical operators, like not, 65 00:03:48,300 --> 00:03:51,240 and conditionals, like if statements, 66 00:03:51,240 --> 00:03:54,663 to solve a problem in just a few lines of code.