The most efficient way to count None values in a Python list is to use the list.count() method. It accepts a specific value to count, which is None in our case, and returns the numeric value representing the total count.
main_list = [18, None, 20, None, 30, 21, 19, None] # Count the number of None values in the list none_count = main_list.count(None) print(none_count) # Output: 3
The above code shows that our input main_list contains 3 None values, so the output is 3.
None acts as a Null value. None and NaN are different objects.
None means an absence of the object, whereas NaN means (Not a Number), which is an exceptional floating-point value. NaN mostly appears in numerical computations.
Using a generator expression
In the generator expression approach, you iterate through the main list and check if any element is None. For each None element, the expression we will generate expression 1.
At last, we will use the sum() function to add all 1s, effectively counting the number of None values.
main_list = [18, None, 20, None, 30, 21, 19, None] # Using generator expression to count None in the list none_count = sum(1 for item in main_list if item is None) print(none_count) # Output: 3
Using filter() and len()
Using the filter() function, we can use a lambda function that returns an iterator containing only None values.
We then convert that iterator to a list using the list() constructor.
Finally, pass that list to the len() function to get the count of None values.
main_list = [18, None, 20, None, 30, 21, 19, None] # Using a filter() and lambda function none_count = len(list(filter(lambda x: x is None, main_list))) print(none_count) # Output: 3
Even in this approach, you can go faster by using list comprehension like this:
main_list = [18, None, 20, None, 30, 21, 19, None] # Using a list comprehension none_count = len([x for x in main_list if x is None]) print(none_count) # Output: 3
Counting NaN Values
To count NaN values in Python, use “generator expression,” where we efficiently iterate a main list, use conditional filtering and boolean summation, and finally, using the sum() function, count the True boolean values, effectively counting NaN occurrences.
import math
import numpy as np
main_list = [3.14, float('nan'), 42, math.nan, "Hello", np.nan, None, math.nan]
# Using a generator expression to count the number of NaN values in the list
nan_count = sum(math.isnan(x) for x in main_list if isinstance(x, float))
print(nan_count)
# Output: 4
Using the math and numpy modules, we attempted to create NaN (math.nan, np.nan) values in different ways.
In the above code, we can see that the list contains 4 NaN values, which we correctly identified and counted.
Using numpy for large datasets
If you are working with a larger dataset, consider using the “numpy” library for optimization, as it runs faster.
import math
import numpy as np
main_list = [3.14, float('nan'), 42, math.nan, "Hello", np.nan, None, math.nan]
# Filter only numeric values and keep NaNs
numeric_values = [x for x in main_list if isinstance(x, float)]
# Count NaN values in the filtered list
nan_count = np.sum(np.isnan(numeric_values))
print(nan_count)
# Output: 4
In this code, we first filtered a list with numeric values and kept NaNs.
In the next step, we used the np.isnan() method to create an array of True or False values, where non-NaN values became False and NaN values became True.
Finally, counted True values using the np.sum() method.
Counting Both None and NaN
Let’s initialize a list with None and NaN and combine checks for None and NaN using a custom helper function:
import math
import numpy as np
def is_none_or_nan(x):
return x is None or (isinstance(x, float) and math.isnan(x))
main_list = [3.14, float('nan'), 42, math.nan, None, np.nan, None, math.nan]
# Counting the number of None and NaN values in the list
total_count = sum(1 for x in main_list if is_none_or_nan(x))
print(total_count)
# Output: 6
That’s all!



