Welcome back.
Remember this exercise that we did a few videos ago where we had some list with letters and I wanted
you to find the duplicates and return them in a list of duplicates in our case.
We wrote this function with loops that returned B and end because both B and n were duplicates in the
list.
Now this is gonna be a tough one using what you learned about comprehension.
Is there a way to do this neater.
For example I want you to say duplicates like this.
And create a comprehension in here that returns this exact same answer.
B And N so that if I print duplicates just in one line I can find the duplicates.
You can use set comprehension you can use list comprehension dictionary comprehension whatever you need.
Give it a go.
Really think about it.
You can pause the video because otherwise I'm gonna go with the answer.
What I'm going to do is let's remove this and just start here.
Now the first thing I want to do is I want to create a list comprehension.
What should I do here.
Well first I know that I'm going to create a variable so let's just call it X and here I'm not going
to modify any of this.
I'm just going to keep X the way it is.
So X is going to represent a B and C then B then D and I'm going to loop over and say for x in the list
some list I want to only add the items that are repeated so how can I check for that well we can do
an if statement and in here I'm going to say well some list dot count remember count tells us how many
times an item appears in the list.
So we just give it the X parameter so a to let us know Hey how many times that is a appear in the list.
How many times does B appear in the list.
How many times the C does B.
So on and so forth.
And R if statement should check if the number is greater than 1 right.
So let's see what happens when I return this.
If I click run like that I get b b and n all right.
That's good but I want to only have them once.
I only need B and I don't need to know that well I don't need a list of four letters.
So how can we do this.
We know that we can create a set like this right.
I can say set and wrap it in here so that the entire list gets turned into a set.
So we give this to the set and then what happens.
Oh and this needs to be brackets right.
Because this is a function.
So this is the set function that Python gives us.
So if I run this look at that I have an and b almost done because well this is now a set.
I asked you to return me a list.
So what should we do.
Well we now can create a list of that set if I click Run.
There we go.
We have our duplicates all in one line.
Mind you a very long line and maybe this isn't the most readable code but that's pretty cool.
Now this is a tough one.
Maybe you didn't get it the first time around.
It's OK.
This is just something that you have to practice.
You have to do a bunch of problems and you'll get used to eventually as you progress through your career
but that's it for comprehension.
I'll see you in the next video.
By.