Suppose you have 2 lists [Python 3.x]:
x=[2,2,3,4,5,5,6]
y=[2,3,5,9,11]
What I want to accomplish is I want to get the common elements among the 2 lists, without getting repeated elements, only using list comprehension. I hope its possible ?!
In other threads, I have seen the following list comprehension that kinda does the job but returns the repeated elements as well:
>>> x=[2,2,3,4,5,5,6]
>>> y=[2,3,5,9,11]
>>> z=[t for t in x if t in y]
>>> z
[2, 2, 3, 5, 5]
I know there are many solutions to get common elements between 2 lists. The most common solution is doing a set intersection, which gets exactly what I want, without the repeated elements. But I want to accomplish this only using list comprehension, as I said before.
Thanks in advance!