Remove tuple from list of tuples if not containing any character in Python

When working with lists of tuples, you may need to remove tuples that don't contain any alphabetic characters. This can be achieved using list comprehension combined with the any() function and isalpha() method.

A list of tuples contains tuples enclosed in a list, where each tuple can hold heterogeneous data types. List comprehension provides a concise way to filter and transform data based on specific conditions.

Syntax

filtered_list = [(a, b) for a, b in original_list if any(c.isalpha() for c in a)]

Example

Let's remove tuples whose first element doesn't contain any alphabetic characters ?

my_list = [('.', 62), ('Mark', 5),
           ('Paul.', 21), ('.....', 0),
           ('-Jane', 115), ('Jake', 15),
           ('::', 63), ('John', 3), ('--', 1),
           ('there', 82), ('Harold', 100)]

print("Original list:")
print(my_list)

my_result = [(a, b) for a, b in my_list
             if any(c.isalpha() for c in a)]

print("\nAfter removing tuples without alphabetic characters:")
print(my_result)
Original list:
[('.', 62), ('Mark', 5), ('Paul.', 21), ('.....', 0), ('-Jane', 115), ('Jake', 15), ('::', 63), ('John', 3), ('--', 1), ('there', 82), ('Harold', 100)]

After removing tuples without alphabetic characters:
[('Mark', 5), ('Paul.', 21), ('-Jane', 115), ('Jake', 15), ('John', 3), ('there', 82), ('Harold', 100)]

How It Works

The solution uses three key components:

  • List comprehension: Creates a new filtered list
  • any() function: Returns True if at least one character is alphabetic
  • isalpha() method: Checks if a character is alphabetic

Alternative Using Filter Function

my_list = [('.', 62), ('Mark', 5), ('Paul.', 21), ('.....', 0)]

# Using filter function
my_result = list(filter(lambda x: any(c.isalpha() for c in x[0]), my_list))

print("Using filter function:")
print(my_result)
Using filter function:
[('Mark', 5), ('Paul.', 21)]

Comparison

Method Readability Performance Best For
List Comprehension High Fast Simple filtering conditions
Filter Function Medium Fast Complex filtering logic

Conclusion

Use list comprehension with any() and isalpha() to efficiently remove tuples without alphabetic characters. This approach is both readable and performant for filtering operations on lists of tuples.

Updated on: 2026-03-25T17:44:22+05:30

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements