Skip to main content

Command Palette

Search for a command to run...

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

Published
1 min read
DAY 26 of PYTHON top 100 questions : from Basic to Advanced !!
P

Hello, I'm Priya Chakraborty, a dedicated B.Tech student in Electrical Engineering at Siliguri Institute Of Technology. I'm an enthusiastic learner, constantly seeking to expand my skill set and knowledge base. With a solid foundation in programming languages such as C, R, Python, and MySQL, I'm poised to tackle complex technical challenges.

But my passions extend beyond the realm of engineering. I'm also an aspiring content writer, driven by a curiosity to communicate ideas, both technical and non-technical, in a way that captivates and educates.

My journey is defined by a relentless pursuit of self-improvement, coupled with strong communication skills. I believe in the power of continuous learning and the importance of sharing knowledge.

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 !!!

More from this blog