Union of Two or more lists in Python

Here we are going to see the union of two or more lists in Python. Union means taking the elements in the several lists and put them in a single list. We can add two or more two lists in Python. Now we will see how to union two lists and more than two, which is how to add three lists.

As we know, union means adding lists or sets or tuples. In python, the operator “|” is used to union o two sets. The function “union()” is used to join two or more sets or lists. It is not compulsory that the given input must be a set. It can be any iterable object such as a list or tuple.

How does the union() function work?

The union() function returns the new list containing all the elements present in the input. In the union() function, the elements are unique. It returns the elements in ascending order.

What are the syntax, parameter, and return type of union() function?

Syntax

The syntax to add sets

set.union(set1,set2,....)

The syntax to add lists

list(set.union(list1,list2,...)

Parameters

list1- The elements present in list 1

list2 – The elements present in list 2

Returns

output list – The list that contains the elements present is list1 and list2.

Union of two lists with duplicate values

Code

lst1=[2,3,4]
lst2=[4,6,7]
union=list(set().union(lst1,lst2))
print("\nThe union of two list is:",union)

The given code is useful to add elements of two lists. Here we are declaring two lists as lst1 and lst2. Giving the elements in both lists. The list contains the duplicate values. A duplicate value is nothing but repeated values. Union function doesn’t return the duplicate values. The elements in the union functions are unique.

Output

The union of two list is: [2, 3, 4, 6, 7]

Union of two list without duplicate values

Code

lst1=[2,3,4]
lst2=[5,6,7]
union=list(set().union(lst1,lst2))
print("\nThe union of two list is:",union)

The given code is useful to add elements of two lists. Here we are declaring two lists as lst1 and lst2. Giving the elements in both lists. The list doesn’t contain the duplicate value. So it will return all the elements present in both lists.

Output

The union of two list is: [2, 3, 4, 5, 6, 7]

Union of three lists with duplicate values

Code

lst1=[2,3,4]
lst2=[5,6,7]
lst3=[7,4,8]
union=list(set().union(lst1,lst2,lst3))
print("\nThe union of three list is:",union)

The given code is useful to add elements of two lists. Here we are declaring two lists as lst1, lst2, and lst3. Giving the elements in all the lists. The list contains the duplicate values. A duplicate value is nothing but repeated values. Union function doesn’t return the duplicate values. The elements in the union functions are unique.

Output

The union of three list is: [2, 3, 4, 5, 6, 7, 8]

Union of three list without duplicate values.

Code

lst1=[2,3,4]
lst2=[5,6,7]
lst3=[1,8,9]
union=list(set().union(lst1,lst2,lst3))
print("\nThe union of three list is:",union)

The given code is useful to add elements of two lists. Here we are declaring two lists as lst1, lst2, and lst3. Giving the elements in all the lists. The list doesn’t contain the duplicate value. So it will return all the elements present in all the lists.

Output

The union of three list is: [1, 2, 3, 4, 5, 6, 7, 8, 9]

1. What is the syntax for the union() function?

The syntax for union() function is
list(set.union(list1,list2,…)

2. What is the purpose of the union() function?

The function “union()” is used to join two or more sets of lists.

3.What does the union() function do in python?

The union() function returns the new list containing all the elements present in the input.

4. Does the union() function returns the duplicate value?

No, the union function does not return the duplicate value. The elements in the union function are unique.

5.Which operator is used for union function?

The “|” operator is used for the union function.

6. What is mean by duplicate value?

Duplicate value means repeated values in the list.

Conclusion

Here we have seen the union of two or more lists in Python. It is possible to union a set and tuples also. In most of the conditions, union function is needed. The above shown is just an example of a union of the list. We can do something more using the union function. It is a built-in function available in a python library.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments