Quick answer: NaN is a floating-point missing marker that is not equal to itself. To remove it from a Python list, use math.isnan() for scalar numeric values and a type-aware guard for mixed lists; use np.isnan() for arrays and pandas dropna() for labeled tabular data. Do not compare every value to float(‘nan’).

NaN means “not a number”. In Python data cleanup, removing NaN from a list means keeping the real numeric items and filtering out floating-point missing markers.
The main references are Python’s math.isnan() documentation, NumPy’s isnan documentation, and pandas Series.dropna().
The main gotcha is that NaN does not compare equal to itself. A filter such as x != float("nan") does not work reliably. Use math.isnan(), np.isnan(), or pandas missing-data tools instead.
Choose the tool based on the data structure. Use math.isnan() for plain Python floats, NumPy masks for arrays, and pandas dropna() for Series or DataFrame columns.
Before removing anything, decide whether NaN means bad input, missing measurement, or a value that should be filled later. Dropping entries changes list length and can break alignment with another list.
If positions matter, keep indexes or clean related lists together. Removing NaN from one list while leaving another list unchanged can pair the wrong records later.
For model inputs, charts, or CSV rows, that alignment is often more important than the individual list itself. Clean complete records whenever possible.
Remove NaN With math.isnan()
For a plain list of floats, use a list comprehension with math.isnan().
import math
values = [1.0, math.nan, 2.5, float("nan"), 4.0]
clean = [item for item in values if not math.isnan(item)]
print(clean)
This keeps only items that are not NaN.
math.isnan() expects a real number. If the list can contain strings or other objects, check the type first or use a safer helper.
This approach creates a new list. That is usually safer than editing the original list while looping over it.
If another part of the program still needs the original list, returning a new cleaned list avoids hidden side effects.
Handle Mixed Lists Safely
When a list mixes numbers and non-numeric items, guard the NaN test.
import math
def is_nan_number(item):
return isinstance(item, float) and math.isnan(item)
values = [1.0, "missing", float("nan"), None, 3.5]
clean = [item for item in values if not is_nan_number(item)]
print(clean)
This removes float NaN entries while leaving strings and None unchanged.
If None should also be removed, make that a separate rule so the cleanup policy stays clear.
Mixed lists deserve explicit rules because None, empty strings, and NaN can mean different things. Treating all of them the same may hide useful information.
Use A Loop When Rules Are Complex
A loop is easier to read when several cleanup rules apply.
import math
values = [1.0, math.nan, None, 2.0, float("nan")]
clean = []
for item in values:
if item is None:
continue
if isinstance(item, float) and math.isnan(item):
continue
clean.append(item)
print(clean)
This version removes both None and float NaN values.
Use this style when each skipped case needs a comment, log entry, or separate handling step.
A loop is also helpful when you need to count removed items or collect rejected entries for a quality report.
Use NumPy For Numeric Arrays
NumPy creates a boolean mask with np.isnan().
import numpy as np
values = np.array([1.0, np.nan, 2.5, np.nan, 4.0])
clean = values[~np.isnan(values)]
print(clean)
The mask is true where values are NaN. The ~ operator inverts the mask so only non-NaN values remain.
Use NumPy when the data is already an array or when vectorized numeric cleanup is part of a larger workflow.
For arrays that may also contain positive or negative infinity, combine np.isnan() with np.isfinite() depending on whether infinite values should stay.
Use pandas dropna()
For pandas data, use dropna().
import pandas as pd
series = pd.Series([1.0, float("nan"), 2.5, None, 4.0])
clean = series.dropna()
print(clean.tolist())
Pandas treats both NaN and many other missing markers as missing data, so dropna() is usually the right tool for Series cleanup.
Use pandas when the list is part of tabular data or when you need consistent missing-data handling across columns.
After dropna(), pandas keeps the original index labels unless you reset them. That is useful for tracing rows back to source data, but it can surprise code that expects consecutive positions.
Do Not Use Equality For NaN
NaN has unusual comparison behavior.
import math
value = float("nan")
print(value == value)
print(math.isnan(value))
The equality check is false, even when comparing the same NaN object with itself. That is why cleanup code should call a NaN test function.
The practical rule is: use math.isnan() for simple float lists, use a helper for mixed lists, use NumPy masks for arrays, and use pandas dropna() for Series or table columns.
For reusable code, write tests for normal numbers, NaN, None, and any non-numeric items your input may contain. Those cases define the cleanup contract.
When memory is tight and the list is large, an in-place rewrite is possible, but it is easier to get wrong. Prefer the clear new-list version until profiling shows it matters.
Filter Numeric Float Values
math.isnan expects a real number, so call it only after handling non-numeric values or using a policy for them. A list comprehension keeps the simple numeric case readable.
import math
values = [1.0, float("nan"), 2.5, 3.0]
clean = [value for value in values if not math.isnan(value)]
print(clean)
Handle Mixed Lists Safely
A list can contain strings, None, integers, and floats. Check the type or catch the narrow conversion error before calling isnan, and decide whether non-numeric values should be retained, rejected, or handled separately.
import math
values = [1.0, "missing", None, float("nan"), 2.0]
clean = []
for value in values:
if isinstance(value, float) and math.isnan(value):
continue
clean.append(value)
print(clean)
Use A NumPy Mask For Arrays
NumPy performs the check element by element and returns a boolean mask. Select the values where isnan is false, or use nan-aware reductions when you do not need to materialize a cleaned array.
import numpy as np
values = np.array([1.0, np.nan, 2.0])
clean = values[~np.isnan(values)]
print(clean)
Use pandas For Labeled Data
pandas represents missing values across columns and provides dropna with axis, subset, and how policies. Use it when the list is really part of a table and the missing-data rule needs column context.
import pandas as pd
frame = pd.DataFrame({"score": [10.0, None, 12.0]})
print(frame.dropna(subset=["score"]))
Use Python’s official math.isnan reference, NumPy’s isnan reference, and pandas’ dropna documentation. Related array cleanup includes NumPy quantiles and counting boolean masks.
For related missing-data and numeric cleanup, compare normalizing NumPy arrays, NumPy quantile(), and counting valid masks before dropping values without a documented rule.
Frequently Asked Questions
How do I remove NaN from a Python list?
Use math.isnan() for numeric float values, with a type-aware guard for mixed lists, then keep only values that are not NaN.
Why does value == float(‘nan’) not work?
NaN is defined so that it is not equal to itself, so compare with math.isnan(), numpy.isnan(), or a library’s missing-data operation instead.
How do I remove NaN from a NumPy array?
Use np.isnan() to build a boolean mask and select the values where the mask is false, or use an appropriate nan-aware array operation.
Should I use pandas for a list with missing values?
Use pandas when the data is already tabular or labeled and dropna(), nullable dtypes, and column-aware missing-data rules are useful.