In Python, the easiest way to get the union of two lists is by using the + operator to add the two lists together. You can then remove the duplicates from the result by converting it to a set, and then converting that set back to a list.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
union_of_lists = list(set(list_1 + list_2))
print(union_of_lists)
#Output:
[1, 2, 3, 4, 5, 8, 9]
If you want to find the union of more than two lists, we can easily do that in Python with a simple function.
def union(lists):
all_elements = []
for x in lists:
all_elements = all_elements + x
return list(set(all_elements))
list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]
list4 = [5,3,6,8,2,2,0]
print(union([list1,list2,list3,list4]))
#Output:
[0, 1, 2, 3, 4, 5, 6, 8, 9]
When working with multiple lists, it can be useful to find the entire collection of elements which exists in all of your lists. The union of two or more lists is a list of all elements from all of the lists.
So, for example, if we have a list A and a list B, then the union of A and B is a list of all elements from both A and B. We combine all items of the two lists with the union.
We can get the union of two lists in Python easily.
To get the union of lists in Python, we can combine lists with the + operator. Then, we can convert the resulting list to a set, and back to a list to get the unique list of all elements in all of the lists.
Below is an example of how to get the union of two lists in Python.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
union_of_lists = list(set(list_1 + list_2))
print(union_of_lists)
#Output:
[1, 2, 3, 4, 5, 8, 9]
The example above gives us a sorted list with no duplicates. If you want to include duplicates, or maintain the order, there are a few other ways you can get the union of a list.
If you’d instead like to get the intersection of multiple lists, you can read our article about finding the intersection of lists.
Finding the Union of Lists and Keep Duplicates in Python
In Python, finding the union of multiple lists and keeping any duplicates is easy.
To combine multiple lists in Python and keep the duplicates of all of the items, you can simply use the + operator.
Below is an example of how to find the union of all lists keeping all duplicates.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
union_of_lists = list_1 + list_2
print(union_of_lists)
#Output:
[5, 3, 8, 2, 1, 9, 3, 4, 2, 1]
If you want to sort this result, then you can use the Python sorted() function or the sort() function.
list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]
union_of_lists = list_1 + list_2
print(sorted(union_of_lists))
#Output:
[1, 1, 2, 2, 3, 3, 4, 5, 8, 9]
How to Find the Union of More Than Two Lists in Python
So far in this article, we’ve only found the union of two lists. We can easily find the union of more than two lists in Python.
To do so, we can define a function which takes in a number of lists, and then finds the union of all of the lists.
Below is an example of how to find the union of more than two lists in Python.
def union(lists):
all_elements = []
for x in lists:
all_elements = all_elements + x
return list(set(all_elements))
list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]
list4 = [5,3,6,8,2,2,0]
print(union([list1,list2,list3,list4]))
#Output:
[0, 1, 2, 3, 4, 5, 6, 8, 9]
Another method to find the union of more than two lists using Python is if you know how many lists you are working with. The example above is good for any number of lists, but if you have three lists, you can use the set union() function.
Below is another example of how to find the union of multiple lists with Python.
def union(list1,list2,list3):
union_of_lists = list(set().union(list1, list2, list3))
return union_of_lists
list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]
print(union(list1,list2,list3))
#Output:
[0, 1, 2, 3, 4, 5, 6, 8, 9]
Hopefully this article has been useful for you to learn how to use Python to get the union of lists.