Welcome back.
Let's do an exercise where we check for duplicates in a list.
So I have some list 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 and 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 songlines say for value in some list and in here while what can
we do.
This is a tough one but what I'm going to do is say if some list dot 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 some list dot count and I give it a.
It's going to come from me.
How many times a 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 an 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 B and N 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 N.
So while we can do here is do another if statement and say if value is not in duplicates and yes this
is a bit of a trick you may remember that not is the opposite right.
So we're checking hate is value and 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 rate.
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 not and conditionals like if statements to solve
a problem in just a few lines of code.