Filter Range Length Tuples in Python

Filtering tuples based on their length within a specific range is a common data processing task in Python. You can filter a list of tuples to keep only those with lengths between minimum and maximum values using various approaches like generator expressions, list comprehensions, loops, or filter functions.

Using Generator Expression

Generator expressions provide memory-efficient filtering by creating an iterator instead of storing all filtered results in memory ?

def filter_range_tuples(tuples, min_len, max_len):
    return tuple(t for t in tuples if min_len <= len(t) <= max_len)

# Create test data
data = [('Python', 'Java'), ('C++', 'JavaScript', 'Go'), ('Ruby',), ('HTML', 'CSS', 'React'), ('Swift',)]

# Filter tuples with length between 1 and 2
filtered_result = filter_range_tuples(data, 1, 2)
print("Filtered tuples:", filtered_result)
Filtered tuples: (('Python', 'Java'), ('Ruby',), ('Swift',))

Using List Comprehension

List comprehensions offer a readable way to filter and create new lists in a single expression ?

def filter_range_tuples(tuples, min_len, max_len):
    return [t for t in tuples if min_len <= len(t) <= max_len]

# Test with different data
subjects = [('Math', 'English'), ('Biology',), ('Physics', 'Chemistry', 'Biology'), ('History',), ('Geography', 'Economics')]

# Filter tuples with length between 2 and 3
filtered_subjects = filter_range_tuples(subjects, 2, 3)
print("Filtered subjects:", filtered_subjects)
Filtered subjects: [('Math', 'English'), ('Physics', 'Chemistry', 'Biology'), ('Geography', 'Economics')]

Using For Loop with append()

Traditional loop approach provides explicit control over the filtering process ?

def filter_range_tuples(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

# Test with number words
numbers = [('one', 'two', 'three'), ('four', 'five'), ('six',), ('seven', 'eight', 'nine'), ('ten', 'eleven')]

# Filter tuples with length between 2 and 3
filtered_numbers = filter_range_tuples(numbers, 2, 3)
print("Filtered numbers:", filtered_numbers)
Filtered numbers: [('one', 'two', 'three'), ('four', 'five'), ('seven', 'eight', 'nine'), ('ten', 'eleven')]

Using filter() and lambda

The filter() function combined with lambda creates functional programming style filtering ?

def filter_range_tuples(tuples, min_len, max_len):
    return list(filter(lambda t: min_len <= len(t) <= max_len, tuples))

# Test with numeric tuples
numbers = [(1, 2), (12, 13, 14), (15,), (18, 19, 20), (30,)]

# Filter tuples with length between 2 and 3
filtered_numbers = filter_range_tuples(numbers, 2, 3)
print("Filtered numbers:", filtered_numbers)
Filtered numbers: [(1, 2), (12, 13, 14), (18, 19, 20)]

Comparison

Method Memory Usage Readability Performance
Generator Expression Low (lazy evaluation) High Fast
List Comprehension Medium High Fast
For Loop Medium Medium Moderate
filter() + lambda Medium Medium Moderate

Conclusion

Use generator expressions for memory-efficient filtering, list comprehensions for readable code, and filter() with lambda for functional programming style. Choose the approach that best fits your specific use case and performance requirements.

Updated on: 2026-03-27T08:10:03+05:30

728 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements