1 00:00:00,880 --> 00:00:01,420 Welcome back. 2 00:00:02,020 --> 00:00:09,850 Let's do an exercise where we check for duplicates in a list, so I have some lists here and I want 3 00:00:09,850 --> 00:00:17,980 you to be able to check this list and print whatever the duplicate values are. 4 00:00:18,670 --> 00:00:19,360 In this case. 5 00:00:19,360 --> 00:00:24,660 We see that and has duplicates and also B, has duplicates. 6 00:00:24,760 --> 00:00:30,130 So I want you to print B and D as a list in here. 7 00:00:31,040 --> 00:00:32,390 How would you go about doing that? 8 00:00:33,690 --> 00:00:37,890 Pause the video, give it a go, otherwise I'm going to keep going with the answer. 9 00:00:38,670 --> 00:00:43,250 And by the way, you're not allowed to use sets for this. 10 00:00:43,980 --> 00:00:48,510 So for this exercise, you can use the data structure sets. 11 00:00:48,510 --> 00:00:53,730 Instead, you have to just use conditionals and loops that we've learned in the past couple of videos. 12 00:00:54,840 --> 00:01:00,080 So give it a try, pause the video and when you're ready, I'm going to go ahead with the answer. 13 00:01:02,010 --> 00:01:07,680 First, I'm going to create a variable called duplicates, and this is going to be an empty list that 14 00:01:07,680 --> 00:01:10,410 we're going to populate with any duplicate values. 15 00:01:11,190 --> 00:01:19,740 Next, I'm going to loop over this list, Songline, to say for value in some list and in here. 16 00:01:20,130 --> 00:01:20,460 Well. 17 00:01:21,800 --> 00:01:22,500 What can we do? 18 00:01:23,330 --> 00:01:34,160 This is a tough one, but what I'm going to do is say if some list dot count, remember, count allows 19 00:01:34,160 --> 00:01:44,140 us to count how many times an item in a list exists and I'm going to say value so that some lists count. 20 00:01:44,270 --> 00:01:50,030 And I give it a it's going to come for me how many times A happens in the list. 21 00:01:50,210 --> 00:01:51,470 Very, very useful. 22 00:01:51,950 --> 00:01:55,190 Again, if you didn't come up with that, there's many solutions to this problem. 23 00:01:55,190 --> 00:01:57,320 So there's no one right answer. 24 00:01:57,330 --> 00:01:59,350 I'm just showing you how I would have done. 25 00:02:00,140 --> 00:02:05,440 Now here I'm going to say if that's greater than one in that case, that means there's a duplicate. 26 00:02:05,900 --> 00:02:09,170 So I'm going to add it inside of the duplicates. 27 00:02:09,950 --> 00:02:17,390 So let's just go ahead and say dot append, because I want to append an in here say value. 28 00:02:19,000 --> 00:02:22,810 And at the end of all this, let's just print duplicates. 29 00:02:25,070 --> 00:02:28,010 Now, let's see if this works, I'm going to click run. 30 00:02:29,680 --> 00:02:32,050 And all right, I get both be. 31 00:02:33,150 --> 00:02:40,550 And end, but I don't need this duplicates, I just need to know what are the duplicates. 32 00:02:40,560 --> 00:02:43,950 I just need one instance of be in one instance of. 33 00:02:44,910 --> 00:02:56,460 So while we can do here is do another if statement and say if the value is not in duplicates. 34 00:02:58,670 --> 00:03:05,900 And yes, this is a bit of a trick, you may remember, that not is the opposite, right? 35 00:03:05,930 --> 00:03:08,750 So we're checking hay is value and duplicates. 36 00:03:09,740 --> 00:03:15,170 And not essentially says, hey, if value is not in duplicates, it reads like English. 37 00:03:16,370 --> 00:03:18,800 And this is going to evaluate to true and false. 38 00:03:20,360 --> 00:03:25,200 And then finally, make sure that we get the indentation right, we're going to append the value. 39 00:03:25,850 --> 00:03:27,170 So if I click run. 40 00:03:29,510 --> 00:03:31,880 I get B and and. 41 00:03:32,980 --> 00:03:38,800 Again, if he didn't get this right away, don't worry, it can get tricky, and this is one of those 42 00:03:38,800 --> 00:03:40,800 things that you just need to keep practicing. 43 00:03:41,110 --> 00:03:49,900 But you see here that I was able to use Loop's I was able to use logical operators like not and conditionals 44 00:03:49,900 --> 00:03:54,520 like if statements to solve a problem in just a few lines of code.