Skip to main content

How to Check If a List Contains Only Numbers in Python

Validating that a list contains only numeric values is a common requirement in data processing, especially when preparing data for mathematical operations or statistical analysis. Python allows for flexible, mixed-type lists, so ensuring type consistency often requires explicit checks.

This guide explores the most efficient ways to verify if a list consists solely of numbers using isinstance(), the all() function, and handling edge cases like empty lists.

The most Pythonic and robust way to check if all elements in a list are numbers is to use the all() built-in function combined with a generator expression.

  • isinstance(x, (int, float)): Returns True if x is either an integer or a float.
  • all(iterable): Returns True only if every item in the iterable is Truthy.
def check_numeric_list(data):
"""Checks if a list contains only integers or floats."""
return all(isinstance(x, (int, float)) for x in data)

# Test Data
valid_list = [1, 2.5, 3, 4.2]
invalid_list = [1, 2, "3"]

print(f"Valid list is numeric? {check_numeric_list(valid_list)}")
print(f"Invalid list is numeric? {check_numeric_list(invalid_list)}")

Output:

Valid list is numeric? True
Invalid list is numeric? False

Method 2: Using Type Hints (Development Time)

While isinstance checks types at runtime (when the code runs), you can use Python's Type Hints to enforce this during development. Static analysis tools (like mypy) will flag errors if you try to put a string into a list intended for numbers.

from typing import List, Union

# This hint tells developers/tools that this list should only contain int or float
numbers: List[Union[int, float]] = [1, 2.5, 3]

# ⛔️ Incorrect: Static analyzer would flag this
# numbers.append("hello")
note

Type hints do not stop the code from running or crashing if invalid data is inserted at runtime. They are documentation and developer aids. Runtime checks (Method 1) are still required for external data validation.

Edge Cases: Handling Empty Lists and Booleans

Empty Lists

The all() function returns True for an empty iterable because "no element is False". Depending on your logic, an empty list might be considered valid (it contains no non-numbers) or invalid (it contains no numbers at all).

empty_data = []

# Standard behavior: Returns True
print(f"Is empty list numeric? {all(isinstance(x, (int, float)) for x in empty_data)}")

# Strict behavior: Ensure list is not empty AND all numeric
is_strictly_numeric = bool(empty_data) and all(isinstance(x, (int, float)) for x in empty_data)
print(f"Is strictly numeric? {is_strictly_numeric}")

Output:

Is empty list numeric? True
Is strictly numeric? False

Booleans

In Python, bool is a subclass of int. Therefore, isinstance(True, int) returns True. If you want to strictly exclude booleans, you must check for them explicitly.

data_with_bool = [1, 2, True]

# Standard check treats bool as number
print(f"Standard check: {all(isinstance(x, (int, float)) for x in data_with_bool)}")

# Strict check excluding bool
# Note: Since bools ARE ints, we must check 'not isinstance(x, bool)'
strict_check = all(isinstance(x, (int, float)) and not isinstance(x, bool) for x in data_with_bool)
print(f"Strict check: {strict_check}")

Output:

Standard check: True
Strict check: False

Conclusion

To validate if a list contains only numbers:

  1. Use all(isinstance(x, (int, float)) for x in list) for the standard runtime check.
  2. Exclude bool explicitly if you want to prevent True/False from passing as numbers.
  3. Decide on Empty Lists: Determine if an empty list should return True or False for your specific use case.