Intersection of Lists
Find the common elements between two lists.
BeginnerTopic: List Programs
Python Intersection of Lists Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to find intersection of two lists
list1 = input("Enter first list elements: ").split()
list2 = input("Enter second list elements: ").split()
intersection = list(set(list1) & set(list2))
print("Intersection:", intersection)Output
Enter first list elements: 1 2 3 Enter second list elements: 2 3 4 Intersection: ['2', '3']
Understanding Intersection of Lists
We convert both lists to sets and use the & operator to compute their intersection.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.