Python Program to Find Union of Two Lists
In this tutorial, we will discuss a Python program to find union of two lists.
Before going to the program first, let us understand what a is Union.
Union:
- The union of two lists contains all unique elements from both lists.
Related: Python program to find Union of two lists
Program code to find the union of two lists in Python
# Find the Union of Two Lists in Python def find_union(lst1, lst2): return list(set(lst1) | set(lst2)) list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] union = find_union(list1, list2) print("Union of the two lists:", union)
Explanation
- Function Definition: The
find_union
function takes two lists as input and returns a list containing all unique elements from both lists. - Main Program: The program defines two lists and finds the union using the
find_union
function. It then prints the result.
Output
- When you run the above program, it will find the union of the two lists and print the result.
Conclusion
- In this tutorial, we learned how to find the union of two lists using a Python program.
- Understanding this concept is essential for data manipulation and enhancing your programming skills.