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']
To perform the union of two lists in python, we just have to create an output list that should contain elements from both the input lists. For instance, if we have list1=[1,2,3,4,5,6] and list2=[2,4,6,8,10,12] , the union of list1 and list2 will be [1,2,3,4,5,6,8,10,12] .
Union of a list means, we must take all the elements from list A and list B (there can be more than two lists) and put them inside a single new list. There are various orders in which we can combine the lists.
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']
>>> list(set(a).union(b)) ['Orange and Banana', 'Orange Banana', 'Grapes']
Thanks @abarnert
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With