Union of Lists
Find the union of two lists (unique elements from both).
BeginnerTopic: List Programs
Python Union of Lists Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to find union of two lists
list1 = input("Enter first list elements: ").split()
list2 = input("Enter second list elements: ").split()
union = list(set(list1) | set(list2))
print("Union:", union)Output
Enter first list elements: 1 2 3 Enter second list elements: 3 4 5 Union: ['1', '2', '3', '4', '5']
Understanding Union of Lists
We use set union (|) to collect all distinct elements from both lists.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.