Union of the list using sets will Lists without repetition of common elements in Python. Union of two lists means it will contain all the elements of both lists.
Example union list of sets in Python
Simple example code union of two lists using set.
l1 = [1, 2, 3, 4, 5] l2 = [2, 4, 6, 8, 10] res = list(set(l1 + l2)) print(res)
Using Union method.
l1 = [1, 2, 3, 4, 5] l2 = [2, 4, 6, 8, 10] res = list(set(l1).union(set(l2))) print(res)
Do comment if you have any doubts or suggestions on this Python Union topic.
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.