How to Find the Union of Two Lists in Python

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

This topic is straightforward and easy to understand. By following the instructions provided, you will be able to complete it with ease. The program will guide you step by step through the process of finding the union of two lists. 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. # MAIN LOOP
  2. while True:
  3.     print("\n============= Find the Union of Two Lists =============\n")
  4.  
  5.     # Input for list 1
  6.     try:
  7.         num1 = int(input('Enter size of list 1: '))
  8.         if num1 < 0:
  9.             print("Size cannot be negative.")
  10.             continue
  11.     except ValueError:
  12.         print("Invalid input.")
  13.         continue
  14.  
  15.     l1 = []
  16.     for _ in range(num1):
  17.         try:
  18.             l1.append(int(input('Enter any number: ')))
  19.         except ValueError:
  20.             print("Invalid number.")
  21.             break
  22.     else:
  23.         # Input for list 2
  24.         try:
  25.             num2 = int(input('Enter size of list 2: '))
  26.             if num2 < 0:
  27.                 print("Size cannot be negative.")
  28.                 continue
  29.         except ValueError:
  30.             print("Invalid input.")
  31.             continue
  32.  
  33.         l2 = []
  34.         for _ in range(num2):
  35.             try:
  36.                 l2.append(int(input('Enter any number: ')))
  37.             except ValueError:
  38.                 print("Invalid number.")
  39.                 break
  40.         else:
  41.             # Compute union
  42.             union = list(set().union(l1, l2))
  43.  
  44.             print("\nList 1:", l1)
  45.             print("List 2:", l2)
  46.             print("Union:", union)
  47.  
  48.     # Try Again Option
  49.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  50.     if opt == 'no':
  51.         print("Exiting program...")
  52.         break
  53.     elif opt != 'yes':
  54.         print("Invalid choice. Exiting program...")
  55.         break

This Python program computes the union of two lists by combining their elements and removing duplicates using a set. It interactively asks the user to input the sizes and elements of two lists, validates the inputs, and then displays both lists along with their union. The program runs in a loop, allowing users to repeat the process or exit.

Output:

There you have it we successfully created How to Find the Union 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