Python Program to Find the Intersection of Two Lists

Introduction

The intersection of two lists refers to the elements that are common to both lists. This operation is useful in various scenarios, such as finding common items between two datasets. This tutorial will guide you through creating a Python program that finds the intersection of two lists.

Example:

  • Input:

    • List 1: [1, 2, 3, 4, 5]
    • List 2: [4, 5, 6, 7, 8]
  • Output: [4, 5]

  • Input:

    • List 1: [10, 20, 30, 40]
    • List 2: [30, 40, 50, 60]
  • Output: [30, 40]

Problem Statement

Create a Python program that:

  • Takes two lists as input.
  • Finds the intersection of these two lists.
  • Displays the elements that are common to both lists.

Solution Steps

  1. Take Input from the User: Use the input() function to get two lists of numbers from the user.
  2. Convert Input to Lists of Integers: Convert the input strings to lists of integers.
  3. Find the Intersection: Use set operations or list comprehensions to find the common elements between the two lists.
  4. Display the Intersection: Use the print() function to display the intersection of the lists.

Python Program

# Python Program to Find the Intersection of Two Lists
# Author: https://www.rameshfadatare.com/

# Step 1: Take input for the two lists from the user
input_list1 = input("Enter the first list of numbers separated by spaces: ")
input_list2 = input("Enter the second list of numbers separated by spaces: ")

# Step 2: Convert the input strings to lists of integers
list1 = list(map(int, input_list1.split()))
list2 = list(map(int, input_list2.split()))

# Step 3: Find the intersection of the two lists using set operations
intersection = list(set(list1) & set(list2))

# Step 4: Display the intersection
print(f"The intersection of the two lists is: {intersection}")

Explanation

Step 1: Take Input for the Two Lists from the User

  • The input() function prompts the user to enter two lists of numbers, separated by spaces. The inputs are stored as strings in the variables input_list1 and input_list2.

Step 2: Convert Input to Lists of Integers

  • The split() method is used to split each input string into individual components, and map(int, ...) is used to convert these components into integers. The list() function then creates two lists of these integers, list1 and list2.

Step 3: Find the Intersection of the Two Lists

  • The intersection of the two lists is found using set operations. The & operator is used to find the common elements between the two sets created from list1 and list2. The resulting set is then converted back to a list.

Step 4: Display the Intersection

  • The print() function is used to display the intersection of the two lists.

Output Example

Example 1:

Enter the first list of numbers separated by spaces: 1 2 3 4 5
Enter the second list of numbers separated by spaces: 4 5 6 7 8
The intersection of the two lists is: [4, 5]

Example 2:

Enter the first list of numbers separated by spaces: 10 20 30 40
Enter the second list of numbers separated by spaces: 30 40 50 60
The intersection of the two lists is: [30, 40]

Example 3:

Enter the first list of numbers separated by spaces: 5 10 15 20
Enter the second list of numbers separated by spaces: 20 25 30 35
The intersection of the two lists is: [20]

Conclusion

This Python program demonstrates how to find the intersection of two lists using set operations. This method is efficient and straightforward, making it a valuable technique for comparing datasets and finding common elements in Python.

Leave a Comment

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

Scroll to Top