How to get the union of two lists using list comprehension?

Coddy picture Coddy · Jan 3, 2014 · Viewed 84.6k times · Source

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']

Answer

alvas picture alvas · Jan 3, 2014

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']