Python | Filter Range Length Tuples

In this program, we are given a list of tuples of variable length and a range to filter based on length. We need to create a Python program to filter out the tuples whose length is not in the range.
Submitted by Shivang Yadav, on November 08, 2021

Filtering records of variable length and extracting only fruitful data from the record is quite an important task. Fields like data science, machine learning often require data cleaning before working with them. And such methods are employed to cleanse data. In this program, we will be discussing filtering tuples based on their length, we will be filtering off the tuples whose lengths are not in the given range.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Filtering Range Length Tuples

In this program, we are given a list of tuples and two integer values marking the length range. We will be creating a python program that will filter out all the elements with length not in the given range and return the remaining tuples.

Input:
[(5, 7), (4, 1, 8), (5, 8, 0), (1, 2, 3, 4, 5, 7)] i = 2, j = 3

Output:
[(5, 7), (4, 1, 8), (5, 8, 0)]

For filtering the tuples of a list based on length, we will find the length of each tuple and check if it is within the given range or not. If it is not in the given range, filter it off.

In Python, we can perform a task by doing a sequence of tasks in multiple ways. Here, we will see some ways to filter range length tuples.

Method 1:

One method to solve the problem is by iterating over the list and then for each tuple check whether its length is between the given range or not. We will use list comprehension to perform iteration and comparison. And the len() method to find the length of the tuple.

# Program to filter range length tuples 
# using list comprehension, 
  
#  Initializing and printing the list of Tuple 
tupList =  [(5, 7), (4, 1, 8), (5, 8, 0), (1, 2, 3, 4, 5, 7)]
print("Initial tuple values of the list : " + str(tupList))
  
# Initializing Range
i, j = 2, 3
  
# filtering range length tuples 
filterTuple = [tup for tup in tupList if len(tup) >= i and len(tup) <= j]
      
# printing result
print("Filtered Tuple values of the list : " + str(filterTuple))

Output:

Initial tuple values of the list : [(5, 7), (4, 1, 8), (5, 8, 0), (1, 2, 3, 4, 5, 7)]
Filtered Tuple values of the list : [(5, 7), (4, 1, 8), (5, 8, 0)]

Method 2:

Another approach to solve the problem is by using the filter method using lambda method which will get a filter based on the length of the tuple within the given range.

# Program to Filter Range Length Tuples 
# using filter method, 

# Initializing and printing the list of Tuple 
tupList =  [(5, 7), (4, 1, 8), (5, 8, 0), (1, 2, 3, 4, 5, 7)]
print("Initial tuple values of the list : " + str(tupList))
  
# Initializing Range
i, j = 2, 3
  
# filtering range length tuples 
filterTuple = list(filter(lambda tup: len(tup) >= i and len(tup) <= j, tupList))
      
# printing result
print("Filtered Tuple values of the list : " + str(filterTuple))

Output:

Initial tuple values of the list : [(5, 7), (4, 1, 8), (5, 8, 0), (1, 2, 3, 4, 5, 7)]
Filtered Tuple values of the list : [(5, 7), (4, 1, 8), (5, 8, 0)]

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.