1 00:00:00,930 --> 00:00:01,990 Welcome back. 2 00:00:02,040 --> 00:00:06,110 Let's do an exercise where we check for duplicates in a list. 3 00:00:06,390 --> 00:00:17,160 So I have some list here and I want you to be able to check this list and print whatever the duplicate 4 00:00:17,160 --> 00:00:18,730 values are. 5 00:00:18,750 --> 00:00:24,840 In this case we see that and has duplicates and also B has duplicates. 6 00:00:24,840 --> 00:00:31,070 So I want you to print B and D as a list in here. 7 00:00:31,090 --> 00:00:34,840 How would you go about doing that pause the video. 8 00:00:34,840 --> 00:00:35,910 Give it a go. 9 00:00:35,920 --> 00:00:37,950 Otherwise I'm going to keep going with the answer. 10 00:00:38,770 --> 00:00:44,050 And by the way you're not allowed to use sets for this. 11 00:00:44,050 --> 00:00:51,280 So for this exercise you can use the data structure sets instead you have to just use conditionals and 12 00:00:51,280 --> 00:00:56,390 loops that we've learned in the past couple of videos so give it a try. 13 00:00:56,420 --> 00:01:02,480 Pause the video and when you're ready I'm going to go ahead with the answer first. 14 00:01:02,510 --> 00:01:07,900 I'm going to create a variable called duplicates and this is going to be an empty list that we're going 15 00:01:07,900 --> 00:01:11,210 to populate with any duplicate values. 16 00:01:11,240 --> 00:01:22,180 Next I'm going to loop over this list songlines say for value in some list and in here while what can 17 00:01:22,180 --> 00:01:22,990 we do. 18 00:01:23,380 --> 00:01:34,330 This is a tough one but what I'm going to do is say if some list dot count remember count allows us 19 00:01:34,330 --> 00:01:39,130 to count how many times an item in a list exists. 20 00:01:39,130 --> 00:01:45,550 And I'm going to say value so that some list dot count and I give it a. 21 00:01:45,640 --> 00:01:46,650 It's going to come from me. 22 00:01:46,660 --> 00:01:50,260 How many times a happens in the list. 23 00:01:50,260 --> 00:01:51,920 Very very useful. 24 00:01:52,060 --> 00:01:55,200 Again if you didn't come up with that there's many solutions to this problem. 25 00:01:55,210 --> 00:01:57,310 So there's no one right answer. 26 00:01:57,370 --> 00:02:00,150 I'm just showing you how I would have done it. 27 00:02:00,250 --> 00:02:03,130 Now here I'm going to say if that's greater than one. 28 00:02:03,220 --> 00:02:05,890 In that case that means there's a duplicate. 29 00:02:05,950 --> 00:02:09,740 So I'm going to add it inside of the duplicates. 30 00:02:10,030 --> 00:02:16,390 So let's just go ahead and say dot append because I want to append an in here. 31 00:02:16,420 --> 00:02:25,100 Say value and at the end of all this let's just print duplicates. 32 00:02:25,110 --> 00:02:26,920 Now let's see if this works. 33 00:02:27,030 --> 00:02:30,860 I'm going to click Run and all right. 34 00:02:30,860 --> 00:02:38,110 I get both B and N but I don't need this duplicates. 35 00:02:38,120 --> 00:02:40,550 I just need to know what are the duplicates. 36 00:02:40,550 --> 00:02:44,360 I just need one instance of B in one instance of N. 37 00:02:44,990 --> 00:02:59,750 So while we can do here is do another if statement and say if value is not in duplicates and yes this 38 00:02:59,750 --> 00:03:05,950 is a bit of a trick you may remember that not is the opposite right. 39 00:03:05,960 --> 00:03:14,090 So we're checking hate is value and duplicates and not essentially says hey if value is not in duplicates 40 00:03:14,090 --> 00:03:21,980 it reads like English and this is going to evaluate to TRUE and FALSE and then finally make sure that 41 00:03:21,980 --> 00:03:23,300 we get the indentation rate. 42 00:03:23,360 --> 00:03:25,860 We're going to append the value. 43 00:03:25,940 --> 00:03:37,900 So if I click Run I get B and n again if he didn't get this right away don't worry it can get tricky 44 00:03:37,910 --> 00:03:42,650 and this is one of those things that you just need to keep practicing but you see here that I was able 45 00:03:42,650 --> 00:03:52,400 to use loops I was able to use logical operators like not and conditionals like if statements to solve 46 00:03:52,640 --> 00:03:54,500 a problem in just a few lines of code.