Find Common Elements of Two Lists
Find elements that appear in both lists (preserving list order of the first).
BeginnerTopic: List Programs
Python Find Common Elements of Two Lists Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to find common elements of two lists
list1 = input("Enter first list elements: ").split()
list2 = input("Enter second list elements: ").split()
common = [x for x in list1 if x in list2]
print("Common elements:", common)Output
Enter first list elements: a b c d Enter second list elements: c d e Common elements: ['c', 'd']
Understanding Find Common Elements of Two Lists
We iterate over the first list and select only those elements that also appear in the second list.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.