Open In App

Python - Filter Range Length Tuples

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a list of tuples where each tuple contains multiple elements, and our task is to filter out tuples whose lengths do not fall within a specified range. For example, if we have a = [(1, 2), (3, 4, 5), (6,), (7, 8, 9, 10)] and the range (2, 3), we should keep only tuples with 2 or 3 elements, resulting in [(1, 2), (3, 4, 5)]. Let's discuss different methods to do this in Python.

Using List Comprehension

List comprehension provides a concise and efficient way to filter tuples based on their length.

Python
# Initializing list of tuples  
a = [(1, 2), (3, 4, 5), (6,), (7, 8, 9, 10)]  
min_len, max_len = 2, 3  

# Filtering tuples based on length  
res = [t for t in a if min_len <= len(t) <= max_len]  

print(res)

Output
[(1, 2), (3, 4, 5)]

Explanation:

  • This method iterates through a, keeping only tuples within the length range.
  • It is efficient as it processes elements in a single pass.

Let's explore more ways to filter range length tuples.

Using filter() with a Lambda Function

filter() function provides an alternative functional programming approach to filtering tuples.

Python
# Initializing list of tuples  
a = [(1, 2), (3, 4, 5), (6,), (7, 8, 9, 10)]  
min_len, max_len = 2, 3  

# Filtering tuples using filter()  
res = list(filter(lambda t: min_len <= len(t) <= max_len, a))  

print(res)

Output
[(1, 2), (3, 4, 5)]

Explanation:

  • filter(lambda t: min_len <= len(t) <= max_len, a) selects tuples whose lengths are between min_len and max_len.
  • list(filter(...)) converts the filtered result into a list, producing [(1, 2), (3, 4, 5)].

Using for Loop

Using a for loop and append(), we can manually filter tuples while maintaining readability.

Python
# Initializing list of tuples  
a = [(1, 2), (3, 4, 5), (6,), (7, 8, 9, 10)]  
min_len, max_len = 2, 3  

# Filtering tuples using a loop  
res = []  
for t in a:  
    if min_len <= len(t) <= max_len:  
        res.append(t)  

print(res)

Output
[(1, 2), (3, 4, 5)]

Explanation: The loop iterates through a, checking each tuple’s length and adding valid tuples to result.

Using itertools.compress()

The itertools.compress() function filters elements using a boolean selector, making it useful for complex filtering operations.

Python
from itertools import compress  

# Initializing list of tuples  
a = [(1, 2), (3, 4, 5), (6,), (7, 8, 9, 10)]  
min_len, max_len = 2, 3  

# Creating a boolean mask for valid tuples  
mask = [min_len <= len(t) <= max_len for t in a]  

# Filtering tuples using compress()  
res = list(compress(a, mask))  

print(res)

Output
[(1, 2), (3, 4, 5)]

Explanation:

  • The mask list stores True for tuples within the valid length range.
  • compress() filters a based on the mask, keeping only valid tuples.

Using NumPy for Large Data

NumPy provides an approach for filtering tuples in structured arrays, though it is more useful for large datasets.

Python
import numpy as np  

# Initializing list of tuples  
a = np.array([(1, 2), (3, 4, 5), (6,), (7, 8, 9, 10)], dtype=object)  
min_len, max_len = 2, 3  

# Filtering tuples using NumPy  
res = a[[min_len <= len(t) <= max_len for t in a]]  

print(res)  

Output
[(1, 2) (3, 4, 5)]

Explanation:

  • np.array(..., dtype=object) creates a NumPy array of tuples with different lengths.
  • List comprehension generates a boolean mask, keeping only tuples with lengths between min_len and max_len.
  • a[...] applies the mask, filtering the tuples, resulting in [(1, 2), (3, 4, 5)].

Next Article

Similar Reads