Introduction
The union of two lists refers to a list that contains all unique elements from both lists, combining them without any duplicates. This operation is useful when you need to aggregate data from different sources while ensuring that each item appears only once. This tutorial will guide you through creating a Python program that finds the union of two lists.
Example:
-
Input:
- List 1:
[1, 2, 3, 4, 5]
- List 2:
[4, 5, 6, 7, 8]
- List 1:
-
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
-
Input:
- List 1:
[10, 20, 30, 40]
- List 2:
[30, 40, 50, 60]
- List 1:
-
Output:
[10, 20, 30, 40, 50, 60]
Problem Statement
Create a Python program that:
- Takes two lists as input.
- Finds the union of these two lists.
- Displays the list containing all unique elements from both lists.
Solution Steps
- Take Input from the User: Use the
input()
function to get two lists of numbers from the user. - Convert Input to Lists of Integers: Convert the input strings to lists of integers.
- Find the Union: Use set operations or list comprehensions to combine the elements of the two lists and remove duplicates.
- Display the Union: Use the
print()
function to display the union of the lists.
Python Program
# Python Program to Find the Union of Two Lists
# Author: https://www.rameshfadatare.com/
# Step 1: Take input for the two lists from the user
input_list1 = input("Enter the first list of numbers separated by spaces: ")
input_list2 = input("Enter the second list of numbers separated by spaces: ")
# Step 2: Convert the input strings to lists of integers
list1 = list(map(int, input_list1.split()))
list2 = list(map(int, input_list2.split()))
# Step 3: Find the union of the two lists using set operations
union = list(set(list1) | set(list2))
# Step 4: Display the union
print(f"The union of the two lists is: {union}")
Explanation
Step 1: Take Input for the Two Lists from the User
- The
input()
function prompts the user to enter two lists of numbers, separated by spaces. The inputs are stored as strings in the variablesinput_list1
andinput_list2
.
Step 2: Convert Input to Lists of Integers
- The
split()
method is used to split each input string into individual components, andmap(int, ...)
is used to convert these components into integers. Thelist()
function then creates two lists of these integers,list1
andlist2
.
Step 3: Find the Union of the Two Lists
- The union of the two lists is found using set operations. The
|
operator (union operator) is used to combine the elements from both sets created fromlist1
andlist2
. The resulting set, which contains only unique elements, is then converted back to a list.
Step 4: Display the Union
- The
print()
function is used to display the union of the two lists.
Output Example
Example 1:
Enter the first list of numbers separated by spaces: 1 2 3 4 5
Enter the second list of numbers separated by spaces: 4 5 6 7 8
The union of the two lists is: [1, 2, 3, 4, 5, 6, 7, 8]
Example 2:
Enter the first list of numbers separated by spaces: 10 20 30 40
Enter the second list of numbers separated by spaces: 30 40 50 60
The union of the two lists is: [10, 20, 30, 40, 50, 60]
Example 3:
Enter the first list of numbers separated by spaces: 5 10 15 20
Enter the second list of numbers separated by spaces: 20 25 30 35
The union of the two lists is: [35, 5, 10, 15, 20, 25, 30]
(Note: The order of elements in the output may vary because sets do not maintain order.)
Conclusion
This Python program demonstrates how to find the union of two lists using set operations. This method is efficient and straightforward, making it a valuable technique for combining datasets and ensuring that each element is unique in Python.