Filter Range Length Tuples in Python


A range length is defined by representing range of values that follow the start and end point along with the length. In Python, there are some built-in functions like list(), append(), filter(), and, lambda which can be used to solve the problem based on Filter Range Length Tuples. This type of problem statement normally helps to build the logic of filtering tool.

Syntax

The following syntax is used in the examples −

len()

The len() is the built-in method in Python that returns the length of the object.

append()

The append method in Python is used to add the element at the end of the list.

filter()

The filter() element of Python is used to filter the elements based on specific conditions.

lambda

The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda functions behave when declared with the def keyword.

Using Generator Expression

This program uses a generator expression that is similar to list comprehension. It uses the parenthesis ‘()’ instead of a square bracket ‘[]’ and with the help of this, it will filter the range length tuple.

Example

In the following example, start the program by defining a function filter_rlt() that accepts three parameters- tuples( to receive the values of variable t through calling function), min_len( receive the value 1 that means only one tuple element), max_length( receive the value 2 that contains a maximum of two tuple element).

Next, in the return statement it uses parenthesis that shows the generator expression, and using for loop, it will iterate each tuple from the variable named tuples. To filter the range length of the tuple it will use the comparison operator ‘<=’. Moving ahead to create the original tuple list in the variable t and use of calling function in the variable filter_tuple to pass the parameter value. Finally, print the result with the help of variable filter_rlt.

# create the function
def filter_rlt(tuples, min_len, max_len):
   return tuple(t for t in tuples if min_len <= len(t) <= max_len)
# create the tuple inside the list for filtration
t = [('ATOM', 'C#'), ('C++', 'DOCKER', 'ANACONDA'), ('PYTHON',), ('HTML', 'JAVASCRIPT', 'MERN'), ('MACHINE LEARNING',)]
filter_tuple = filter_rlt(t, 1, 2)
print("THE FILTER TUPLE RANGE LENGTH(Technology & Tools):\n",filter_tuple)

Output

THE FILTER TUPLE RANGE LENGTH(Technology & Tools):
 (('ATOM', 'C#'), ('PYTHON',), ('MACHINE LEARNING',))

Using List Comprehension with a Conditional Expression

The program uses list comprehension by using square brackets and allows conditional expressions that set the if-statement to filter the range length tuples.

Example

In the following example, we will create the recursive function that receives some parameter to work on the return statement by using for loop( iterate the individual tuple list), and an if-statement( check the comparison to filter the range of the tuple ). This process is known as list comprehension. Then it will create the original tuples to set the value as multiple sub-tuples inside the list. The function call is used to call the function and pass the value to work on the filtration of range length of tuples and get the desired result.

def filter_rlt(tuples, min_len, max_len):
   return [t for t in tuples if min_len <= len(t) <= max_len]
# create the sub-tuple list
tuples = [('Math', 'English'), ('Zoology'), ('Physics', 'Chemistry', 'Biology'), ('Social Science',), ('Digital Market'), ('Geography', 'Economics', 'Computer')]
# Calling function 
filter_tuple = filter_rlt(tuples, 2, 3) 
print("The Filter Tuple Range Length(Subjects):\n", filter_tuple)

Output

The Filter Tuple Range Length(Subjects):
 [('Math', 'English'), ('Physics', 'Chemistry', 'Biology'), ('Geography', 'Economics', 'Computer')]

Using for Loop

The program uses for loop to iterate into multiple sub-tuples from the list and filter the range length tuple using append() function.

Example

In the following example, we will first define a function named filter_rlt that accepts three parameters- tuples, min_len, and, max_len that will help to set the range value of tuples. Then create the empty list in the variable filtered_tuples that will store the filter range tuple. Now use the for loop where variable t iterates into the tuples and use of if statement it checks the condition for range comparison and then append the filter tuple to the variable filtered_tuples. Next, return the same variable to get the new tuple list. Moving ahead to create the original tuple list in the variable ts that will used for range calculation. The new variable named filter_tuple that store the value as function calling to pass the parameters- ts(contain the values of tuple list), 2(range for min_len), and, 3(range for max_len). Finally, we are printing the result with the help of variable filter_tuple.

def filter_rlt(tuples, min_len, max_len):
   filtered_tuples = []
   for t in tuples:
      if min_len <= len(t) <= max_len:
         filtered_tuples.append(t)
   return filtered_tuples
#  create the tuple list
ts = [('one', 'two', 'three'), ('four', 'five'), ('six',), ('seven', 'eight', 'nine'), ('ten','eleven')]
filter_tuple = filter_rlt(ts, 2, 3)
print("The Filter Tuple Range Length:\n", filter_tuple)

Output

The Filter Tuple Range Length(Numbers in words):
 [('one', 'two', 'three'), ('four', 'five'), ('seven', 'eight', 'nine'), ('ten', 'eleven')]

Using Filter() and Lambda Function

The program uses the nested structure of built-in functions in Python such as list(), filter(), and, lambda to filter range length tuples.

Example

In the following example, the program defines the function named filter_rlt that accepts three parameters- tuples ()( to receive the value from variable t in the calling function), min_len, and max_len. The minimum and maximum length used to set the range of the tuple list. Next, return the function by using three different built-in methods- list()[create the list], filter()[remove those tuples which are not satisfied according to ranges], and, lambda[it accepts two parameters to calculate the filter result]. Now simple create the original tuple list in the variable t. Then set the value range min and max in the function named filter_rlt and store it in the variable filter_tuple. Finally, print the variable with the help of variable named filter_tuple.

def filter_rlt(tuples, min_len, max_len):
   return list(filter(lambda t: min_len <= len(t) <= max_len, tuples))
# create the sub-tuple list 
t = [(1, 2), (12, 13, 14), (15,), (18, 19, 20), (30,)]
# Set the range value in the calling function parameter
filter_tuple = filter_rlt(t, 2, 3)
print("The Filter Tuple Range Length:", filter_tuple)

Output

The Filter Tuple Range Length: [(1, 2), (12, 13, 14), (18, 19, 20)]

Conclusion

We discussed the various ways to get the solution based on Filter Range Length Tuples. This filtering logic helps to enhance the users ability work with data and solving a variety of problem challenges. By filtering user can extract the spectific information and that can be used to solve the real-life business problem.

Updated on: 17-Jul-2023

223 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements