Q:

Python | Filter Range Length Tuples

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Tuple List Intersection... >>
<< Python | Nested Tuples Subtraction...