Python Program to Find Intersection of Two Lists

In this tutorial, we will discuss a Python program to find intersection of two lists.

Before going to the program first, let us understand what is Intersection.

Intersection:

  • The intersection of two lists contains elements that are common to both lists.

Related: Python Program to Find the Second Largest Number in a List

Program code to find the intersection of two lists in Python

# Find the Intersection of Two Lists in Python
def find_intersection(lst1, lst2):
    return list(set(lst1) & set(lst2))

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
intersection = find_intersection(list1, list2)
print("Intersection of the two lists:", intersection)

Explanation

  1. Function Definition: The find_intersection function takes two lists as input and returns a list containing elements that are common to both lists.
  2. Main Program: The program defines two lists and finds the intersection using the find_intersection function. It then prints the result.

Output

Python Program to Find Intersection of Two Lists

  • When you run the above program, it will find the intersection of the two lists and print the result.

Conclusion

  • In this tutorial, we learned how to find the intersection of two lists using a Python program.
  • Understanding this concept is essential for data manipulation and enhancing your programming skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *