How to Remove All Tuples in a List Outside the Given Range in Python

In this tutorial, we will learn how to program "How to Remove All Tuples in a List Outside the Given Range in Python". The objective is to remove all tuples from a list that fall outside a specified range. This tutorial will guide you step by step through the process of using range conditions to filter the list. 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 removing tuples outside a given range. 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. while True:
  2.     print("\n============ Remove All Tuples in a List Outside the Given Range ============\n")
  3.  
  4.     n = int(input("Enter the number of tuples: "))
  5.     y = []
  6.  
  7.     # Input tuples
  8.     for i in range(n):
  9.         s = input(f"Enter string for tuple {i+1}: ")
  10.         num = input(f"Enter number for tuple {i+1} (e.g., 12CS039): ")
  11.         y.append((s, num))
  12.  
  13.     # Input range
  14.     low = int(input("Enter lower number (e.g., 39 for 12CS039): "))
  15.     up = int(input("Enter upper number (e.g., 100 for 12CS100): "))
  16.  
  17.     # Filter tuples numerically
  18.     p = []
  19.     for item in y:
  20.         num_str = item[1]
  21.         # Extract numeric part: take last 3 digits (assumes numbers <= 999)
  22.         numeric_part = ''.join(filter(str.isdigit, num_str))
  23.         if numeric_part:  # make sure it's not empty
  24.             num_val = int(numeric_part)
  25.             if low <= num_val <= up:
  26.                 p.append(item)
  27.  
  28.     print("\nFiltered tuples within range:")
  29.     print(p)
  30.  
  31.     # Ask user if they want to try again
  32.     opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
  33.     if opt == 'no':
  34.         print("Exiting program...")
  35.         break
  36.     elif opt != 'yes':
  37.         print("Invalid choice. Exiting program...")
  38.         break

This Python program allows the user to filter a list of tuples based on a numeric range. Each tuple consists of a string and a number (or a string containing digits). The program extracts the numeric portion from each tuple, compares it with the user-provided lower and upper limits, and retains only the tuples whose numeric values fall within the specified range. The filtered list is displayed, and the user can choose to repeat the process or exit.

Output:

There you have it we successfully created How to Remove All Tuples in a List Outside the Given Range 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