-2

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!

Adithya
  • 25
  • 3
  • 2
    Since `set` does the job by its very nature, *why* do you want to use only list comprehension? This suggests that you're dumping homework or quiz problems on us without the required effort. – Prune Jul 31 '18 at 15:52
  • 1
    `[x for x in range(0)] or set(x) & set(y)` meeting the requirement for a comprehension ;) – Chris_Rands Jul 31 '18 at 15:53
  • 1
    Don't make it hard on yourself by learning to do something the wrong way. Use the tools available to you that are most appropriate. Use set() and call it a day. – dfundako Jul 31 '18 at 15:54
  • Nope @Prune I dont have any sorta homework right now thats related to this. This was just outta curiosity, coz I dont know very much about list comprehension and there isnt much documentation out there about this. Most of the time I get syntax error when I try anything else than for and if. – Adithya Jul 31 '18 at 15:59

2 Answers2

0

Use set

x=[2,2,3,4,5,5,6]
y=[2,3,5,9,11]
z=list(set([t for t in x if t in y]))
print (z)

Output

[2, 3, 5]
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Ah perfect! Being a beginner I didnt think bout it. Thanks! But still, can we do this right in the list comprehension, without typecasting anything to a set? Just out of curiosity ;) – Adithya Jul 31 '18 at 15:58
  • Yup I need to wait for 3 more mins. – Adithya Jul 31 '18 at 16:01
  • Well here's a solution without using `set`: `answer = [e for e in x if e in y and (y.pop(y.index(e)) or True)]`. Source: https://stackoverflow.com/questions/2864842/common-elements-comparison-between-2-lists – Sheldore Jul 31 '18 at 16:05
0

If you don't mind the extra work, you can step through the range of values:

z = [t for t in range(min(x), max(x)+1) if t in x and t in y]

This iterates over the range of one list, considering each value exactly once.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Perfect. Thanks! This does the job without typecasting *anything* into a set, which is pretty much what I wanted to see. – Adithya Jul 31 '18 at 16:09
  • 1
    Just a word of caution: This will only work for integer elements. If you have `2.0` in `x` and `2` in `y`, this method fails. – Sheldore Jul 31 '18 at 16:11
  • @Bazingaa quite correct; this works only for a discrete, well-ordered data type ... actually only those for which a `range` iteration is defined. – Prune Jul 31 '18 at 16:22