Find Union of Two Lists in Python (3 Examples)

 

Hi! This tutorial will show you how to get the union of two lists in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Example Python Lists of Integers

We will, here, create the example Python lists of integers whose union we will determine in this tutorial.

So, in your preferred Python coding IDE, run the lines of code below to create the example list of integers:

list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
 
 
print(list1)
print(list2)
 
 
# [1, 2, 3, 4]
# [3, 4, 5, 6]
 
 
print(type(list1))
print(type(list2))
 
 
# <class 'list'>
# <class 'list'>

With the example lists of integers created, let us now see examples of how to get the union of the two lists.
 

Example 1: Get Union of Two Lists Using + Operator

In this first example, we will use the + operator to get the union of the two lists:

union_list = list(set(list1 + list2))
 
print(union_list)
 
# [1, 2, 3, 4, 5, 6]
 
print(type(union_list))
 
# <class 'list'>

Here, we first concatenate the two lists using the +, which creates a new list that contains all the elements from both lists. We then convert this concatenated list to a set using the set() function, which automatically removes any duplicate elements. Finally, we convert the resulting set back to a list using the list() function, giving us a list that contains all the unique elements from both input lists.

 

Example 2: Get Union of Two Lists Using union() Method

In this second example, we will use the union() method to find the union of the two lists:

union_set = set(list1).union(set(list2))
 
union_list = list(union_set)
 
print(union_list)
 
# [1, 2, 3, 4, 5, 6]
 
print(type(union_list))
 
# <class 'list'>

In this method, we first convert each input list to a set using the set() function, which removes any duplicates. We then call the union() method on the first set, parsing the second set as an argument.

This method returns a new set containing all the elements that appear in either set. Finally, we convert the resulting set back to a list using the list() function, giving us a list that contains all the unique elements from both input lists.
 

Example 3: Get Union of Two Lists Using for Loop & Conditional if Statement

In this last example, we will use a for loop and a conditional if statement to get the union of the two lists:

union_list = list1.copy()
for item in list2:
    if item not in union_list:
        union_list.append(item)
 
print(union_list)
 
# [1, 2, 3, 4, 5, 6]
 
print(type(union_list))
 
# <class 'list'>

In the above example, we start by creating a new list that is a copy of the first input list. We then loop over each element in the second input list and check if it already appears in union_list using a conditional if statement: if item not in union_list.

If the element is not already in union_list, we append it to the end of the list using the append() method. This way, we add only the elements that do not already exist in union_list, thus we obtain a list that contains all the unique elements from both input lists.

With that, we have demonstrated how to find the union of two lists in Python. I do hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to find the union of two lists in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the union of two lists in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find the union of two lists in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top