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 N because both b.
And MN where duplicates in the list.
Now this is going to 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.
So 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 going to 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, then 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 does A appear in the list?
How many times does B appear in the list?
How many times does C, does B, so on and so forth?
And our if statement should check if the number is greater than one.
Right.
So let's see what happens when I return this if I click Run.
Look at 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 N.
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 out 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 okay.
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.
Bye bye.