Python Program to Find Common Elements in Two Lists
In this tutorial, we will discuss a Python program to find common elements in two lists.
Related: Python Program To Check if Two Strings are Anagrams
Program code to find common elements in two lists in Python
# Find Common Elements in Two Lists in Python def find_common_elements(lst1, lst2): return list(set(lst1) & set(lst2)) list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] common_elements = find_common_elements(list1, list2) print("Common elements in the two lists:", common_elements)
Explanation
- Function Definition: The
find_common_elements
function takes two lists as input and returns a list containing elements that are common to both lists. - Main Program: The program defines two lists and finds the common elements using the
find_common_elements
function. It then prints the result.
Output
- When you run the above program, it will find the common elements in the two lists and print the result.
Conclusion
- In this tutorial, we learned a Python program for finding common elements in two lists.
- Understanding this concept is essential for data manipulation and enhancing your programming skills.