22.4 C
New York
Sunday, April 28, 2024
HomeProgrammingPythonHow To Find Intersection Of Two Lists In Python

How To Find Intersection Of Two Lists In Python

Discover how to easily find the intersection of two lists in Python using simple and efficient methods. Enhance your programming skills today!

In this tutorial, we will discuss the commonly used methods for the intersection of any two lists in Python programming language, along with a sample example & its output.

The list is one of the essential built-in data types in the Python program that works as a complete Data Structure.

In this structure, one can add different data types simultaneously, like Numbers, Strings, other Lists, etc. The intersection of any two objects means the particular point where those two objects align with each other.

In simple terms, the two objects’ with everyday belongings will be marked as their intersection point. The same thing happens for the list in the Python program, also.

How To Find Intersection Of Two Lists In Python

how to find intersection of two lists In python

Consider the following example where two lists are declared with some values.

L1 = [1, 2, 3] # First List Declaration
L2 = [3, 4, 5] # Second List Declaration

In this case, the intersection point will be [3]. Because the number [3] is common in both lists declared. The ways to find an intersection of two lists are listed below:

  • Using Counter() Method
  • Using Loop Method
  • Using Set() Method

Let us find out each method with one sample example & its output.

Method 1: Find Out The Intersection Of Two Lists Using Counter() Method

The collection is an essential package with some built-in functions in the Python program. A similar inbuilt function is the Counter() method. The counter method takes the object name, in this case, the list name & finds out similarities.

Below are the steps that used to implement the program:

  • First, import the necessary collection package in the program to use the counter() method.
  • Two different lists have been declared in the program, along with some values.
  • The intersection point between those two lists will be received using the counter() method. And the result will be stored.
  • The result will now be converted to the list & printed in the program.

Code:

import collections # Importing Collections

l1 = [10, 20, 30, 40] # Declaration Of First List
l2 = [30, 40, 50, 60] # Declaration Of Second List

# Getting The Intersection Point
res = collections.Counter(l1) & collections.Counter(l2) 
print("The Intersection Point Is: ",list(res.elements()))

You might know clearly about the code where we saw printing values using the Counter() method.

However, if you face issues understanding the code, you can always get professional Python assistance from experts online.

Now, let us find the output of the above code snippet to understand the counter() method implementation process.

Output:

The Intersection Point Is: [30, 40]

…Program finished with exit code 0
Press ENTER to exit console.

Method 2: Find Out The Intersection Of Two List Using The Loop Method

The concept is to use the one-loop method along with one conditional statement. The loop will check each & every element of the list. If the same element is present in the second list, then the element will be added to one empty list.

Below are the steps that used to implement the program:

  • Two Lists have been declared with string values in them.
  • One for each loop is implemented to check every first list element.
  • Inside that list, the conditional statement is used to check the presence of the same element in the second list.
  • If the element exists in the second list, it will be added to the empty list. Otherwise, the for loop will execute the next iteration.
  • That declared empty list will now be printed in the program.

Code:

l1 = ["C", "C++", "Java"] # Declaration Of First List
l2 = ["C++", "Java", "Python"] # Declaration Of Second List

res = [] # Empty List

for ele in l1: # For Loop Implementation 
   if ele in l2:
          res.append(ele) # Adding Elements

print("The Intersection Point Is: " ,res)

Let us find the output of the above code snippet to understand the loop method implementation process.

Output:

The Intersection Point Is: [‘C++’, ‘Java’]

…Program finished with exit code 0
Press ENTER to exit console.

Method 3: Find Out The Intersection Of Two Lists Using Set() Method

The set() method is another inbuilt function in the Python programming language developed to handle such mathematical issues. To use the set() method, we don’t have to import any packages to the program; it will usually work.

Below are the steps that used to implement the program:

  • Two lists have been declared with some random integer values with some commonness.
  • Now, in one separate variable, use the set method. Use the set() method for two lists & join them with the logical AND. It will help to get the common values.
  • The data will be converted to the list in the same statement. The result will now be printed in the program.

Code:

l1 = [10, 20, 30, 60] # Declaration Of First List
l2 = [30, 40, 50, 60] # Declaration Of Second List

res = list(set(l1) & set(l2)) # Implementing Set To Get Intersection
print("The Intersection Point Is: ", res)

Let us find the output of the above code snippet to understand the set() method implementation process.

Output:

The Intersection Point Is: [60, 30]

…Program finished with exit code 0
Press ENTER to exit console.

Conclusion

As we saw, the intersection of any two lists is very easy to find. You need to have a basic knowledge of Python programming.

In this article, we discussed three easy methods to find the intersection of two lists in Python. The intersection of any two lists can be found in whatever its elements.

As per your choice, you can select any one of the methods & start practicing it. It will help you in the future.

Himanshu Tyagi
Himanshu Tyagi
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator. With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!
RELATED ARTICLES