Find Common Elements of Two Lists

Find elements that appear in both lists (preserving list order of the first).

PythonBeginner
Python
# 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']

We iterate over the first list and select only those elements that also appear in the second list.