Last modified: May 30, 2026

Remove NaN from Python List

Dealing with missing data is common in Python.

NaN stands for "Not a Number".

It often appears in data science projects.

You need to remove NaN values to clean your list.

This guide shows you simple ways to do it.

What is NaN in Python?

NaN is a special floating-point value.

It represents missing or undefined data.

Python's float('nan') creates a NaN.

Standard lists can hold NaN values.

But NaN can break calculations.

So removing them is important.

Method 1: Use List Comprehension

List comprehension is clean and fast.

You can check each item with math.isnan().

This method works for float NaN values.

 
import math

my_list = [1, 2, float('nan'), 4, float('nan'), 6]

# Remove NaN using list comprehension
clean_list = [x for x in my_list if not math.isnan(x)]

print(clean_list)

[1, 2, 4, 6]

Important:math.isnan() only works on numeric values.

Use this method when you have floats.

Method 2: Use filter() Function

The filter() function is another option.

It applies a condition to each element.

It returns a filtered iterator.

 
import math

my_list = [3.0, float('nan'), 7.5, float('nan'), 9.2]

# Use filter to remove NaN
clean_list = list(filter(lambda x: not math.isnan(x), my_list))

print(clean_list)

[3.0, 7.5, 9.2]

This method works well with lambda functions.

It is concise and readable.

Method 3: Use pandas for DataFrames

If you work with pandas, use dropna().

This is best for large datasets.

But for simple lists, stick with Python.

 
import pandas as pd
import numpy as np

# Create a pandas Series with NaN
series = pd.Series([1, np.nan, 3, np.nan, 5])

# Remove NaN
clean_series = series.dropna()

print(clean_series.tolist())

[1.0, 3.0, 5.0]

Use pandas when you have structured data.

For raw lists, use list comprehension.

Method 4: Check with numpy.isnan()

NumPy also has a isnan() function.

It works well with arrays.

You can apply it to lists too.

 
import numpy as np

my_list = [10, float('nan'), 20, float('nan'), 30]

# Convert to numpy array and filter
arr = np.array(my_list)
clean_arr = arr[~np.isnan(arr)]

print(clean_arr.tolist())

[10.0, 20.0, 30.0]

NumPy is fast for numerical data.

But it adds an extra dependency.

What About None Values?

NaN is not the same as None.

None is Python's null value.

To remove None, use a different check.

 
my_list = [1, None, 3, None, 5]

# Remove None values
clean_list = [x for x in my_list if x is not None]

print(clean_list)

[1, 3, 5]

Always know your data type.

This helps you choose the right method.

Common Pitfalls

NaN is not equal to itself.

So x == float('nan') always returns False.

Always use math.isnan() or numpy.isnan().

Do not use equality checks for NaN.

Performance Tips

List comprehension is fastest for small lists.

NumPy is best for large numeric arrays.

Pandas is ideal for data frames.

Choose based on your use case.

Internal Links

Learn more about removing items by index.

Check the count method guide for list sizes.

Read the insert complete guide for adding items.

Conclusion

Removing NaN from a Python list is easy.

Use list comprehension with math.isnan() for simple lists.

Use filter() for a functional approach.

Use NumPy or pandas for larger data.

Always avoid equality checks on NaN.

Clean data leads to better results.

Start cleaning your lists today.