Today we are going to share a Python Program to find the intersection of two lists. If you are a python beginner and want to start learning the python programming, then keep your close attention in this tutorial as I am going to share a Python Program to find the intersection of two lists.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def intersection(a, b): return list(set(a) & set(b)) def main(): alist=[] blist=[] n1=int(input("Enter number of elements for list1:")) n2=int(input("Enter number of elements for list2:")) print("For list1:") for x in range(0,n1): element=int(input("Enter element" + str(x+1) + ":")) alist.append(element) print("For list2:") for x in range(0,n2): element=int(input("Enter element" + str(x+1) + ":")) blist.append(element) print("The intersection is :") print(intersection(alist, blist)) main() |
Enter number of elements for list1:3
Enter number of elements for list2:4
For list1:
Enter element1:34
Enter element2:23
Enter element3:65
For list2:
Enter element1:33
Enter element2:65
Enter element3:23
Enter element4:86
The intersection is : [65, 23]
To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.