Skip to content

Union of two lists Python | Example code

  • by

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.

Example union of two lists Python

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:

Union of two lists 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 (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.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.