Welcome back.
Let's do an exercise where we check for duplicates in a list.
So I have some lists here, and I want you to be able to check this list and print whatever the duplicate
values are.
In this case, we see that DN has duplicates and also B has duplicates.
So I want you to print B and D as a list in here.
How would you go about doing that?
Pause the video.
Give it a go.
Otherwise, I'm going to keep going with the answer.
And by the way, you're not allowed to use sets for this.
So for this exercise, you can use the data structure sets.
Instead, you have to just use conditionals and loops that we've learned in the past couple of videos.
So give it a try.
Pause the video and when you're ready, I'm going to go ahead with the answer.
First I'm going to create a variable called duplicates, and this is going to be an empty list that
we're going to populate with any duplicate values.
Next, I'm going to loop over this list, so I'm going to say four value and some list.
And in here.
Well.
What can we do?
This is a tough one.
But what I'm going to do is say if some list does not count, remember, count allows us to count how
many times an item in a list exists.
And I'm going to say value.
So that sum list count, and I give it a.
It's going to come from me.
How many times a day happens in the list?
Very, very useful.
Again, if you didn't come up with that, there's many solutions to this problem.
So there's no one right answer.
I'm just showing you how I would have done it.
Now, here, I'm going to say if that's greater than one, in that case, that means there's a duplicate.
So I'm going to add it inside of the duplicates.
So let's just go ahead and say dot append, because I want to append and in here say value.
And at the end of all this, let's just print duplicates.
Now.
Let's see if this works.
I'm going to click Run.
And.
All right, I get both be.
And MN.
But I don't need this duplicates.
I just need to know what are the duplicates?
I just need one instance of B in one instance of NW.
So what we can do here is do another if statement and say if the value.
Is not in duplicate.
And yes, this is a bit of a trick.
You may remember that knot is the opposite, right?
So we're checking.
Hey, is value in duplicates?
And not essentially says, hey, if value is not in duplicates, it reads like English.
And this is going to evaluate to true and false.
And then finally make sure that we get the indentation right, we're going to append the value.
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.
And this is one of those things that you just need to keep practicing.
But you see here that I was able to use loops, I was able to use logical operators like knot and conditionals,
like if statements to solve a problem in just a few lines of code.