52

Consider the following lists:

a = ['Orange and Banana', 'Orange Banana']
b = ['Grapes', 'Orange Banana']

How to get the following result:

c = ['Orange and Banana', 'Orange Banana', 'Grapes']
user2864740
  • 60,010
  • 15
  • 145
  • 220
Coddy
  • 881
  • 4
  • 10
  • 16
  • Who voted to close as off-topic? – recursive Jan 02 '14 at 23:35
  • @recursive I did not, but it sure is offtopic. – alko Jan 02 '14 at 23:35
  • 1
    my question is specifically about list comprehension. that post doesn't deal the question with list comprehension. there is an answer, but it is wrong – Coddy Jan 02 '14 at 23:41
  • 1
    [ x for x in b if x in a ] this doesn't work – Coddy Jan 02 '14 at 23:42
  • 2
    If you're asking "How do I do this with a list comprehension, whether or not a list comprehension is a reasonable way to do this", that's a paradigm [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). And if you're _not_ asking that, it's a dup. – abarnert Jan 02 '14 at 23:47
  • Voting to re-open because the OP *explicitly* is asking how to do this with list comprehension: 1) title/tag 2) "my question is specifically about list comprehension. that post doesn't deal the question with list comprehension. there is an answer, but it is wrong" – user2864740 Jan 02 '14 at 23:49
  • @user2864740: In that case, the answer is "don't do this with a list comprehension, do it the way that answer says". – abarnert Jan 02 '14 at 23:50
  • @abarnert Which is a poor answer - but a good addendum/note. – user2864740 Jan 02 '14 at 23:50
  • @user2864740: That list comprehension will give you the intersection, not the union. – abarnert Jan 02 '14 at 23:50
  • @user2864740: A question for which the only good answer is a poor answer is a question that should be closed. – abarnert Jan 02 '14 at 23:51
  • 1
    "My Y is right" is too prevalent on SO. The OP *knows* about Y but is *explicitly asking about X*. – user2864740 Jan 02 '14 at 23:51
  • it give this: ['Orange Banana'] – Coddy Jan 02 '14 at 23:52
  • 1
    @Coddy I think it's a fair question, but could be asked differently - too much XY juice in this post. Start with the non-working list-comprehension, explain what it does (as abarnert pointed out it's the intersection), and what is desired. At least then it might get some consideration .. – user2864740 Jan 02 '14 at 23:53
  • 1
    You can do `a + [x for x in b if x not in a]`, but this is incredibly stupid. Or you could just do `[x for x in ]`, or something even dumber. But there is nothing in listcomp syntax that maps to unioning things together in any reasonable way, so there is no non-stupid answer to this question. – abarnert Jan 02 '14 at 23:54

2 Answers2

91

If you have more than 2 list, you should use:

>>> a = ['Orange and Banana', 'Orange Banana']
>>> b = ['Grapes', 'Orange Banana']
>>> c = ['Foobanana', 'Orange and Banana']
>>> list(set().union(a,b,c))
['Orange and Banana', 'Foobanana', 'Orange Banana', 'Grapes']
alvas
  • 115,346
  • 109
  • 446
  • 738
  • 3
    Why put `a, b, c` into a list just so you can explode that list again? Just do `set().union(a, b, c)`. – abarnert Jan 02 '14 at 23:49
  • @abarnert, BTW, great point on `next(takewhile(not_, count()))` =) – alvas Jan 03 '14 at 00:28
  • 3
    By using `set()`, you're eliminating duplicates from the lists. Notice `'Orange Banana'` appears twice in the input, but only once in the output. Sometimes this may be desirable, sometimes not. It's an important characteristic to note, and we really need to know how to do both ways. – Edward Ned Harvey Oct 12 '17 at 22:42
  • 3
    ```set(a + b + c)``` Should be faster as it requires less operations – Aus_10 Dec 31 '19 at 19:14
21
>>> list(set(a).union(b))
['Orange and Banana', 'Orange Banana', 'Grapes']

Thanks @abarnert

CT Zhu
  • 52,648
  • 17
  • 120
  • 133