11. Problem to get common elements from two lists.

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
  1. Take 2 lists as input and create an empty list.
  2. Using for loop and if statement check whether an element from list1 exists in list2.
  3. If it exists then add it to the empty list.
  4. 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

split the list with even,odd values

List in reverse order using for loop

Leave a Comment