DAY 26 of PYTHON top 100 questions : from Basic to Advanced !!

DAY 26 of PYTHON top 100 questions : from Basic to Advanced !!

Write a Python program to find the common elements between three given lists:

list1 = [1, 2, 3, 4, 5]
list2 = [3, 4, 5, 6, 7]
list3 = [5, 6, 7, 8, 9]
set1 = set(list1)
set2 = set(list2)
set3 = set(list3)
common_elements = set1.intersection(set2, set3)
common_elements_list = list(common_elements)
print("Common elements:", common_elements_list)

Output :


Common elements: [5]

This Python program finds the common elements between three given lists: list1, list2, and list3. It does this by converting the lists into sets to efficiently handle duplicate elements, then uses the intersection method to find the common elements among the sets. The resulting common elements are stored in the common_elements set. If you need the result as a list, you can convert the common_elements set back into a list and store it in common_elements_list. Finally, the program prints the common elements found in the three lists.


If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).


HAPPY LEARNING !!!