Lists are one of the most used data structures in Python. Sometimes, we might need to find the common elements between any two given lists. In this article, we will discuss how we can perform the intersection operation on two lists in Python to find the common elements between them.
How to Perform List Intersection in Python?
To perform the intersection of two lists in Python, we just have to create an output list that should contain elements that are present in both the input lists. For instance, if we have list1=[1,2,3,4,5,6]
and list2=[2,4,6,8,10,12]
, the intersection of list1
and list2
will be [2,4,6]
. You can observe that each element of the output list belongs to both list1
and list2
. In other words, The output list contains only those elements that are present in both the input lists.
We can perform the intersection of lists in Python using various approaches. Let us discuss them one by one.
List Intersection in Python Using a For Loop
To perform the intersection of two lists, we will first create an empty list named newList
to store the elements of the output list. After that, we will traverse the first input list and check if its elements are present in the second input list or not. For this, we will use the membership operator (in operator). If an element is present in both lists, we will append the element to the newList
. After execution of the for loop, we will get all the elements that are present in both the input lists in the newList
.
list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
newList = []
for element in list1:
if element in list2:
newList.append(element)
print("Intersection of the lists is:", newList)
Output:
First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Intersection of the lists is: [2, 4, 6]
Alternatively, you can traverse the second input list and check if its elements are present in the first input list or not. Then, you can add elements to the newList
based on the presence of the elements as follows.
list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
newList = []
for element in list2:
if element in list1:
newList.append(element)
print("Intersection of the lists is:", newList)
Output:
First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Intersection of the lists is: [2, 4, 6]
I would suggest you traverse the smaller list using the for loop. This will make your code more efficient.
Intersection of Lists in Python Using Sets
Operations like union and intersection have been originally defined for sets. We can also use sets to find the intersection of two lists. To perform the intersection operation on lists by using sets, you can convert the input lists to sets using the set() function. The set() function takes a list as its input and returns a set containing unique elements in the list.
After creating the sets, you can perform the set intersection operation using the intersection()
method. The intersection() method, when invoked on a set, takes another set as its input argument. After execution, it returns a set containing the common elements in both sets.
Once we get the intersection set, we can convert the output set back into a list as follows.
list1 = [1, 2, 3, 4, 5, 6]
print("First list is:", list1)
list2 = [2, 4, 6, 8, 10, 12]
print("Second list is:", list2)
set1 = set(list1)
set2 = set(list2)
newList = list(set1.intersection(set2))
print("Intersection of the lists is:", newList)
Output:
First list is: [1, 2, 3, 4, 5, 6]
Second list is: [2, 4, 6, 8, 10, 12]
Intersection of the lists is: [2, 4, 6]
Conclusion
In this article, we have discussed how we can perform the intersection of lists in Python. To know more about lists, you can read this article on list comprehension. You might also like this article on how to reverse a list in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.