What is union of two lists in python?

Use plus “+” operator to Union of two lists in Python. Union of two lists means combining two lists into one, all the elements from list A and list B, and putting them inside a single new list.

Simple example code with Union list with reflecting the repetition.

lst1 = [1, 2, 3, 4]
lst2 = [5, 6, 1]

new_list = lst1 + lst2

print(new_list)

Output:

What is union of two lists in python?

Without Repetition

we use the set() function on both the lists, individually and then use the union function or union operator.

lst1 = [1, 2, 3, 4]
lst2 = [5, 6, 1]

# final_list = list(set(lst1) | set(lst2))
new_list = list(set(lst1).union(set(lst2)))

print(new_list)

Output: [1, 2, 3, 4, 5, 6]

Do comment if you have any doubts and suggestions on this Python list.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

What is union of two lists in python?

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Perform a union, keeping repetition:

>>> c = a + b
[1, 2, 2, 3, 2, 5, 6]

Perform a union, keeping repetition & order:

>>> c = sorted(a + b)
[1, 2, 2, 2, 3, 5, 6]

Perform an union, no repetition in each list, but repetition allowed in final union, and keeped order:

>>> c = sorted(list(set(a)) + list(set(b)))
[1, 2, 2, 3, 5, 6]

After clarification of the question, the goal is to build a list that take elements (including repetition) of, and then add elements of b if they are not in the new list.

>>> c = a + [e for e in b if e not in a]
[1, 2, 2, 2, 3, 5, 6]

After another clarification of the question, the goal is to build a list containing all elements of input list. But, if elements are in common, they are pushed the same number there

>>> from collections import Counter
>>> def merge(a,b):
...   na, nb = Counter(a), Counter(b)
...   return list(Counter({k: max((na[k], nb[k])) for k in set(a + b)}).elements())
>>> merge([1, 2, 2, 2, 3], [2, 5, 6])
[1, 2, 2, 2, 3, 5, 6]
>>> merge([1, 2, 3], [2, 2, 4])
[1, 2, 2, 4]

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?
  • What are the syntax, parameter, and return type of union() function?
    • Syntax
    • Parameters
    • Returns
  • Union of two lists with duplicate values
    • Code
  • Union of two list without duplicate values
    • Code
  • Union of three lists with duplicate values
    • Code
  • Union of three list without duplicate values.
    • Code
  • FAQs Related to Python Union of Lists
  • Conclusion
  • Trending Right Now

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.

  • Easily Convert Unix time to Datetime in Python

  • The A-Z of Python Virtualenv Location

  • 6 Methods To Tokenize String In Python

  • Recommended Ways to Print To Stderr in Python

How do you take the union of two lists in Python?

Use plus “+” operator to Union of two lists in Python. Union of two lists means combining two lists into one, all the elements from list A and list B, and putting them inside a single new list.

What is a union in Python?

Python Set union() Method The union() method returns a set that contains all items from the original set, and all items from the specified set(s).

What are 3 different ways to combine 2 lists in Python?

The following are the 6 ways to concatenate lists in Python..
concatenation (+) operator..
Naive Method..
List Comprehension..
extend() method..
'*' operator..
itertools.chain() method..

Is list () and [] the same in Python?

In practical terms there's no difference. I'd expect [] to be faster, because it does not involve a global lookup followed by a function call. Other than that, it's the same. Compare list = int; list() with list = int; [] .