Skip to content

How to Check If a Tuple Contains Only Numbers in Python

Learn how to validate Python tuples for numeric values (int/float) using all() and isinstance(), including handling edge cases like empty tuples and boolean exclusions.

Tuples in Python are immutable sequences often used to store fixed collections of data. A common validation task in data processing involves ensuring that a tuple consists entirely of numeric values (integers and floating-point numbers) before performing mathematical operations.

This guide explains how to validate numeric tuples using Python's built-in all() and isinstance() functions, and how to handle specific edge cases like empty tuples and boolean values.

Understanding Numeric Tuples

A numeric tuple contains only int (integers) or float (decimals) values. Because tuples are immutable, you cannot change an element if you discover it is the wrong type; you must validate the tuple as a whole.

int_tuple = (1, 2, 3)
float_tuple = (1.5, 2.5, 3.5)
mixed_tuple = (1, 2.5, 3)

invalid_tuple = (1, 2, "three")

The most Pythonic way to check the contents of an iterable is using the all() function combined with a generator expression.

  • isinstance(x, (int, float)): Checks if x is either an integer or a float.
  • all(...): Returns True only if every item in the iteration is True.
def is_numeric(tup):
    return all(isinstance(x, (int, float)) for x in tup)

data_valid = (10, 20.5, 30)
data_invalid = (10, "20", 30)

print(f"Is valid tuple numeric? {is_numeric(data_valid)}")
print(f"Is invalid tuple numeric? {is_numeric(data_invalid)}")

Output:

Is valid tuple numeric? True
Is invalid tuple numeric? False

Method 2: Handling Empty Tuples

In Python, the all() function returns True for an empty iterable. This concept is known as "vacuous truth" (there are no elements that are not numbers). However, depending on your application logic, an empty tuple might be considered invalid.

You should explicitly define how to handle empty tuples.

empty_tuple = ()

print(f"Default check: {all(isinstance(x, (int, float)) for x in empty_tuple)}")

def is_numeric_strict(tup):
    if not tup:
        return False
    return all(isinstance(x, (int, float)) for x in tup)

print(f"Strict check: {is_numeric_strict(empty_tuple)}")

Output:

Default check: True
Strict check: False

Method 3: Excluding Booleans (Strict Check)

A common pitfall in Python is that the bool type is a subclass of int. This means isinstance(True, int) returns True. If your data validation requires strictly numbers (and not logical flags like True/False), you must explicitly exclude bool.

bool_tuple = (1, 5, True) # 'True' is technically an integer (1)

print(f"Standard check: {all(isinstance(x, (int, float)) for x in bool_tuple)}")

def is_strictly_number(tup):
    return all(
        isinstance(x, (int, float)) and not isinstance(x, bool) 
        for x in tup
    )

print(f"Strict check: {is_strictly_number(bool_tuple)}")

Output:

Standard check: True
Strict check: False

Conclusion

To check if a tuple contains only numbers:

  1. Use all() with isinstance() for the standard, most efficient check.
  2. Decide on Empty Tuples: Determine if () should be valid (True) or invalid (False) for your specific use case.
  3. Watch out for Booleans: Remember that True and False pass integer checks unless explicitly excluded.
Navigation