A store manager I work with loaded one week of daily online sales into a Python list from a broken export. Two values came through as NaN, so her weekly total printed nan instead of a dollar amount.
Her first fix was x != float("nan"), which removes nothing, because NaN never equals anything, not even itself.
Quick answer: keep every value that math.isnan() says isn’t NaN:
import math
clean = [x for x in sales if not math.isnan(x)]
In this tutorial, I’ll show you how to remove NaN values from a list in Python with math.isnan(), the no-import x == x check, NumPy, pandas, and a helper for lists that also hold None and text.
How do you remove NaN values from a list with math.isnan()?
Every example uses the same seven days of sales, 03/02/2026 to 03/08/2026, run on Python 3.12.5. A list comprehension keeps the values where math.isnan() returns False:
import math
sales = [1249.50, math.nan, 987.25, 1410.00, math.nan, 1102.75, 865.40]
clean = [x for x in sales if not math.isnan(x)]
print(clean)
print(f"Days kept: {len(clean)} of {len(sales)}")
print(f"Total with NaN: {sum(sales)}")
print(f"Total without NaN: ${sum(clean):,.2f}")
Output:
[1249.5, 987.25, 1410.0, 1102.75, 865.4]
Days kept: 5 of 7
Total with NaN: nan
Total without NaN: $5,614.90

Five days are left, totaling $5,614.90. math.isnan() is also how you check if a variable is NaN, so it reads clearly.
Remove NaN without imports: the x == x check and filter()
NaN is the only float that isn’t equal to itself, so x == x is False only for NaN. That gives you a filter with no import at all:
nan = float("nan")
sales = [1249.50, nan, 987.25, 1410.00, nan, 1102.75, 865.40]
print(nan == nan)
print([x for x in sales if x == x])
print(list(filter(lambda x: x == x, sales)))
print([x for x in sales if x != nan])
Output:
False
[1249.5, 987.25, 1410.0, 1102.75, 865.4]
[1249.5, 987.25, 1410.0, 1102.75, 865.4]
[1249.5, nan, 987.25, 1410.0, nan, 1102.75, 865.4]
Both filters return the same five values. The last line is the intro’s trap: x != nan is True for every item.
I still prefer math.isnan() in shared code; x == x makes readers stop and think.
Remove NaN in place and keep a matching list aligned
Here each sale has a date in a second list, so I filter the pairs together with zip(), then clean the original list in place with slice assignment:
import math
days = ["03/02", "03/03", "03/04", "03/05", "03/06", "03/07", "03/08"]
sales = [1249.50, math.nan, 987.25, 1410.00, math.nan, 1102.75, 865.40]
pairs = [(d, s) for d, s in zip(days, sales) if not math.isnan(s)]
for day, amount in pairs:
print(f"{day}/2026 ${amount:>9,.2f}")
sales[:] = [s for s in sales if not math.isnan(s)]
print(len(sales), "values left in the same list object")
Output:
03/02/2026 $ 1,249.50
03/04/2026 $ 987.25
03/05/2026 $ 1,410.00
03/07/2026 $ 1,102.75
03/08/2026 $ 865.40
5 values left in the same list object
03/03 and 03/06 are gone from the report, and sales[:] = ... changed the original list object. The same pattern helps when you remove multiple elements from a list by a condition.
What if the list mixes None, strings and NaN?
Exports often mix None, the text "nan" and numbers stored as text. math.isnan() only accepts numbers:
import math
raw = [1249.50, None, 987.25, "nan", 1410.00, math.nan, "865.40"]
clean = [x for x in raw if not math.isnan(x)]
Output:
TypeError: must be real number, not NoneType
The fix is a small helper that converts each value with float() and returns None for anything that isn’t a real number:
import math
raw = [1249.50, None, 987.25, "nan", 1410.00, math.nan, "865.40"]
def to_amount(value):
try:
amount = float(value)
except (TypeError, ValueError):
return None
return None if math.isnan(amount) else amount
clean = [a for v in raw if (a := to_amount(v)) is not None]
print(clean)
print(f"Kept {len(clean)} of {len(raw)} values")
Output:
[1249.5, 987.25, 1410.0, 865.4]
Kept 4 of 7 values

float("nan") turns the text into a real NaN, so the helper catches it too. If your list only has None gaps, see how to remove None values from a list. The walrus operator := needs Python 3.8+.
Remove NaN from a NumPy array with np.isnan() and np.isfinite()
For big numeric data you’ll want NumPy. np.isnan() builds a True/False mask, ~ flips it, and indexing keeps the rest (NumPy 2.5.3):
import numpy as np
sales = np.array([1249.50, np.nan, 987.25, 1410.00, np.nan, 1102.75, 865.40])
clean = sales[~np.isnan(sales)]
print(clean)
print(clean.tolist())
sales = np.append(sales, np.inf)
print(sales[~np.isnan(sales)])
print(sales[np.isfinite(sales)])
Output:
[1249.5 987.25 1410. 1102.75 865.4 ]
[1249.5, 987.25, 1410.0, 1102.75, 865.4]
[1249.5 987.25 1410. 1102.75 865.4 inf]
[1249.5 987.25 1410. 1102.75 865.4 ]

.tolist() turns the result back into a plain list. When the export also wrote inf, np.isnan() kept it and np.isfinite() dropped it. To build test arrays like this one, see NumPy NaN arrays.
A 2-D array needs one more step. To drop every row that has a NaN anywhere, add .any(axis=1):
import numpy as np
# orders, revenue per store for 03/02/2026
stores = np.array([
[42, 1249.50],
[np.nan, 987.25],
[37, np.nan],
[51, 1410.00],
])
print(stores[~np.isnan(stores).any(axis=1)])
Output:
[[ 42. 1249.5]
[ 51. 1410. ]]
Only the two complete stores are left. For more mask tricks, see NumPy filtering.
Drop NaN, None and pd.NA with pandas dropna()
Wrap the list in a pandas Series. dropna() treats None, float("nan") and pd.NA all as missing (pandas 3.0.6):
import pandas as pd
raw = [1249.50, None, 987.25, float("nan"), 1410.00, pd.NA]
s = pd.Series(raw)
print(s.isna().tolist())
print(s.dropna().tolist())
Output:
[False, True, False, True, False, True]
[1249.5, 987.25, 1410.0]

For whole tables, drop rows with NaN in pandas works the same way. When a gap should count as zero instead, replace NaN with 0.
Common errors when removing NaN from a list
TypeError from math.isnan(). TypeError: must be real number, not NoneType means the list holds None or text. Use the to_amount() helper or pandas.
list.remove() can’t find NaN. sales.remove(float("nan")) raises ValueError: list.remove(x): x not in list, because a new NaN never equals the one in the list. Filter instead.
Removing items inside a for loop. It skips elements, so you’ll miss a NaN that follows another. Build a new list or use slice assignment.
Working on other list cleanups? These go well with this one:
- Check if a number is NaN in Python
- Remove empty strings from a list
- Remove duplicates from a list in Python
- Convert a NumPy array to a list
Frequently asked questions
Why is NaN not equal to itself in Python?
Python floats follow IEEE 754, which makes NaN unequal to every value, itself included.
Does removing NaN change the list length?
Yes, seven values became five here. Filter matching lists together with zip().
How do I replace NaN with 0 instead of removing it?
Use [0.0 if math.isnan(x) else x for x in sales]. The list keeps its length, but averages will include the zeros.
How do I remove the string “nan” from a list?
Text isn’t a float, so math.isnan() raises TypeError. Convert with float() first, as the to_amount() helper does.
Which method should I use for a large list?
NumPy, because the mask runs in compiled code. For small lists, the math.isnan() comprehension needs no install.
Where is math.isnan() documented?
See the official math module documentation for math.isnan() and math.isfinite().

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn