Python program to find common elements in three lists using sets



In this article, we will learn how to find common elements in three lists. The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type. However, Set is a collection in Python, which is unordered, unchangeable, and unindexed.

Let’s say we have the following input −

a = [5, 10, 15, 20, 25]
b = [2, 5, 6, 7, 10, 15, 18, 20]
c = [10, 20, 30, 40, 50, 60]

The following should be the output displaying common elements −

[10, 20]

Find common elements in three lists using sets with intersection()

The intersection() method is used in Python to find common elements in three lists −

Example

def IntersectionFunc(myArr1, myArr2, myArr3): s1 = set(myArr1) s2 = set(myArr2) s3 = set(myArr3) # Intersection set1 = s1.intersection(s2) output_set = set1.intersection(s3) # Output set set to list endList = list(output_set) print(endList) # Driver Code if __name__ == '__main__' : # Elements of 3 arrays myArr1 = [5, 10, 15, 20, 25] myArr2 = [2, 5, 6, 7, 10, 15, 18, 20] myArr3 = [10, 20, 30, 40, 50, 60] # Calling Function IntersectionFunc(myArr1, myArr2, myArr3)

Output

[10, 20]

Find common elements in three lists using set()

To find common elements in three lists, we can use the set() method −

Example

# Elements of 3 arrays myArr1 = [5, 10, 15, 20, 25] myArr2 = [2, 5, 6, 7, 10, 15, 18, 20] myArr3 = [10, 20, 30, 40, 50, 60] print("First = ",myArr1) print("Second = ",myArr2) print("Third = ",myArr3) # Finding common elements using set output_set = set(myArr1) & set(myArr2) & set(myArr3); # Converting the output into a list final_list = list(output_set) print("Common elements = ",final_list)

Output

First =  [5, 10, 15, 20, 25]
Second =  [2, 5, 6, 7, 10, 15, 18, 20]
Third =  [10, 20, 30, 40, 50, 60]
Common elements =  [10, 20]

Find common elements in three lists using sets by taking user input

We can find the common elements in three lists by taking user input as well −

Example

def common_ele(my_A, my_B, my_C):
	my_s1 = set(my_A)
	my_s2 = set(my_B)
	my_s3 = set(my_C)
	my_set1 = my_s1.intersection(my_s2)
	output_set = my_set1.intersection(my_s3)
	output_list = list(output_set)
	print(output_list)
if __name__ == '__main__' :
# First List
A=list()
n=int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(n)):
	p=int(input("Size="))
	A.append(int(p))
	print (A)
	# Second List
	B=list()
n1=int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(n1)):
	p=int(input("Size="))
	B.append(int(p))
	print (B)
	# Third Array
	C=list()
n2=int(input("Enter the size of the List"))
print("Enter the number")
for i in range(int(n2)):
	p=int(input("Size="))
	C.append(int(p))
	print (C)
	# Calling Function
	common_ele(A, B, C)

Output

Enter the size of the List 3
Enter the number
Size= 2
[2]
Size= 1
[2, 1]
Size= 2
[2, 1, 2]
Enter the size of the List 3
Enter the number
Size= 2
[2]
Size= 1
[2, 1]
Size= 4
[2, 1, 4]
Enter the size of the List 4
Enter the number
Size= 3
[3]
[]
Size= 2
[3, 2]
[2]
Size= 1
[3, 2, 1]
[1, 2]
Size= 3
[3, 2, 1, 3]
[1, 2]

Advertisements