We use lists in almost every program in python. Sometimes, we might need to combine two lists. It is possible that the lists that need to be combined might have some common elements. To avoid including duplicate elements in the resultant list, we can perform the union of the given lists. In this article, we will discuss what union operation on a list means and how we can perform the union of two lists in python.
How to Perform the Union of Lists in Python?
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]
. You can observe that each element of the output list either belongs to list1
or list2
. In other words, each element from list1 as well as list2 is present in the output list.
We can perform the union of lists in python using various approaches. Let us discuss them one by one.
Union of Lists in Python using For Loop
To perform union of lists using a for loop, we will first create an empty list named newList
to store the values of the output list. After that, we will add all the elements of the first input list to newList
using the extend()
method. Now, we have to add those elements of the second input list into the newList
that are not already present in it. For this, we will traverse each element of the second list and check if it exists in the newList
or not. If the element isn’t already present in the newList
, we will append the element to the newList
using the append()
method. After traversing the second input list, we will get the list containing the union of both the input lists as the newList
. You can observe this in the following example.
list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
newList = []
newList.extend(list1)
for element in list2:
if element not in newList:
newList.append(element)
print("Union of the lists is:", newList)
Output:
First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Union of the lists is: [1, 2, 3, 4, 5, 6, 8, 10, 12]
You can optimize the above approach if you are allowed to modify the input lists. To optimize the program, you can simply check if the elements in the second input list are present in the first input list or not using a for loop. If the element is not present, we can append the element to the first input list. After execution of the for loop, we will get the union of both the lists in the first input list as shown below.
list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
for element in list2:
if element not in list1:
list1.append(element)
print("Union of the lists is:", list1)
Output:
First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Union of the lists is: [1, 2, 3, 4, 5, 6, 8, 10, 12]
Alternatively, you can store the union of the lists in the second input list as follows.
list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
for element in list1:
if element not in list2:
list2.append(element)
print("Union of the lists is:", list2)
Output:
First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Union of the lists is: [2, 4, 6, 8, 10, 12, 1, 3, 5]
Union of Lists in Python using Sets
Operations like union and intersection have been originally defined for sets. We can also use sets to find the union of two lists in python. To perform union operation on lists by using sets, you can convert the input lists to sets. After that, you can perform the set union operation using the union()
method. Finally, you can convert the output set back into a list as follows.
list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
set1 = set(list1)
set2 = set(list2)
newList = list(set1.union(set2))
print("Union of the lists is:", newList)
Output:
First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Union of the lists is: [1, 2, 3, 4, 5, 6, 8, 10, 12]
Conclusion
In this article, we have discussed how we can perform the union of lists in python. To know more about lists, you can read this article on list comprehension. You might also like this article on how to reverse a list in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.