Python - Filter Tuples with All Even Elements

Filtering tuples with all even elements is a common operation in Python data processing. This involves checking if every element in each tuple is divisible by 2 and keeping only those tuples that satisfy this condition.

Using List Comprehension with all()

The most Pythonic approach combines list comprehension with the all() function to filter tuples where every element is even ?

tuple_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,), (2, 4, 6)]

result = [tup for tup in tuple_list if all(ele % 2 == 0 for ele in tup)]
print("Original tuples:", tuple_list)
print("Filtered tuples with all even elements:", result)
Original tuples: [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,), (2, 4, 6)]
Filtered tuples with all even elements: [(6, 4, 2, 8), (8, 0, 2), (2, 4, 6)]

Using filter() with lambda

The filter() function provides a functional programming approach to solve this problem ?

tuple_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,), (2, 4, 6)]

result = list(filter(lambda tup: all(ele % 2 == 0 for ele in tup), tuple_list))
print("Original tuples:", tuple_list)
print("Filtered tuples:", result)
Original tuples: [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,), (2, 4, 6)]
Filtered tuples: [(6, 4, 2, 8), (8, 0, 2), (2, 4, 6)]

Using a Custom Function

For more complex filtering logic, you can create a custom function ?

def filter_all_even_tuples(tuple_list):
    """Filter tuples containing only even numbers"""
    filtered_tuples = []
    for tup in tuple_list:
        if all(element % 2 == 0 for element in tup):
            filtered_tuples.append(tup)
    return filtered_tuples

tuple_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,), (2, 4, 6)]
result = filter_all_even_tuples(tuple_list)

print("Original tuples:", tuple_list)
print("Filtered tuples:", result)
Original tuples: [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7,), (2, 4, 6)]
Filtered tuples: [(6, 4, 2, 8), (8, 0, 2), (2, 4, 6)]

Handling Edge Cases

It's important to consider edge cases like empty tuples or tuples with negative numbers ?

tuple_list = [(6, 4, 2), (), (-2, 4, 6), (1, 3, 5), (-1, -3)]

result = [tup for tup in tuple_list if all(ele % 2 == 0 for ele in tup)]
print("Original tuples:", tuple_list)
print("Filtered tuples:", result)
print("Note: Empty tuple is included because all() returns True for empty iterables")
Original tuples: [(6, 4, 2), (), (-2, 4, 6), (1, 3, 5), (-1, -3)]
Filtered tuples: [(6, 4, 2), (), (-2, 4, 6)]
Note: Empty tuple is included because all() returns True for empty iterables

Performance Comparison

Method Readability Performance Best For
List Comprehension High Fast Most cases
filter() + lambda Medium Good Functional programming style
Custom Function High Good Complex logic or reusability

Conclusion

List comprehension with all() is the most efficient and readable method for filtering tuples with all even elements. Use filter() for functional programming approaches or custom functions when additional logic is required.

Updated on: 2026-03-27T12:48:07+05:30

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements