In this program, we will take two lists as input and find the common elements between both lists with the help of the below-given steps.
Common elements from two lists:
Steps to solve the program
- Take 2 lists as input and create an empty list.
- Using for loop and if statement check whether an element from list1 exists in list2.
- If it exists then add it to the empty list.
- Print the list to see the common element.
#Input list
list1 = [4, 5, 7, 9, 2, 1]
list2 = [2, 5, 8, 3, 4, 7]
#Creating empty list
common_list = []
for value in list1:
if value in list2:
common_list.append(value)
#Printing output
print(common_list)
Output :
[4, 5, 7, 2]
Related Articles
Python program to reverse a list with for loop.
Python program to reverse a list with a while loop.
Python program to reverse a list using index slicing.
Python program to reverse a list with reversed and reverse methods.
Python program to copy or clone one list to another list.
Python program to return True if two lists have any common member.