How to Find the Intersection of Two Lists in Python

In this tutorial, we will learn how to program "How to Find the Intersection of Two Lists in Python". The objective is to find the intersection between two given lists. This tutorial will guide you step by step through the process of finding the intersection. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving skills and improve your coding abilities.

This topic is straightforward and easy to understand. Simply follow the instructions provided, and you’ll complete it with ease. The program will guide you step by step through the process of finding the intersection. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def intersection(a, b):
  2.     return list(set(a) & set(b))
  3.  
  4. def main():
  5.     alist = []
  6.     blist = []
  7.    
  8.     n1 = int(input("Enter number of elements for list 1: "))
  9.     n2 = int(input("Enter number of elements for list 2: "))
  10.  
  11.     print("\nFor list 1:")
  12.     for x in range(n1):
  13.         element = int(input(f"Enter element {x + 1}: "))
  14.         alist.append(element)
  15.  
  16.     print("\nFor list 2:")
  17.     for x in range(n2):
  18.         element = int(input(f"Enter element {x + 1}: "))
  19.         blist.append(element)
  20.  
  21.     print("\nThe intersection is:")
  22.     print(intersection(alist, blist))
  23.  
  24. while True:
  25.     print("\n============== Find the Intersection of Two Lists ==============\n")
  26.  
  27.     main()
  28.  
  29.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  30.     if opt == 'no':
  31.         print("Exiting program...")
  32.         break
  33.     elif opt != 'yes':
  34.         print("Invalid choice. Exiting program...")
  35.         break

This Python program finds the intersection of two lists — the common elements shared between them. It first asks the user for the number of elements in each list and then collects those elements through input. The `intersection()` function uses Python’s `set` data structure to efficiently find shared elements between both lists and returns them as a list. The results are then displayed to the user. The program runs inside a loop, allowing repeated execution until the user chooses to exit.

Output:

There you have it we successfully created How to Find the Intersection of Two Lists in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials