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)
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)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.