Remove NaN values from a Python List

This tutorial will discuss multiple ways to remove nan values from a Python list.

Table Of Contents

Using List Comprehension

Suppose we have a list that contains certain values. Like this,

# Create a List with Some NaN values
sampleList = [11, 22, float('nan'), 43, 23, float('nan'), 35]

We might want to remove all the NaN (Not a Number) values from this list. One way to do this is by using list comprehension. We can iterate over each item of the list with list comprehension and select only those values that are not equal to NaN. This comprehension will create a new list without the NaN values. By assigning this new list to the same variable, it will appear as though we’ve removed all the NaN values from the original list.

Let’s see the complete example,

import math

# Create a List with Some NaN values
sampleList = [11, 22, float('nan'), 43, 23, float('nan'), 35]
print("Original List:", sampleList)

# Using list comprehension to filter out NaN values
sampleList =[x for x in sampleList if not math.isnan(x)]

print("List after removing NaN values:", sampleList)

Output

Original List: [11, 22, nan, 43, 23, nan, 35]
List after removing NaN values: [11, 22, 43, 23, 35]

Using numPy Array

We can convert our list into a NumPy array using the numpy.array() function. After that, we can filter out NaN values from this array using the isnan() function. However, this will convert the values into floats. Therefore, we need to perform a transformation on each of the remaining values.

Afterwards, we can typecast it back to a list. By assigning this new list to the same variable as the original list, it will give the impression that we have removed the NaN values from the list in Python.

Let’s see the complete example,

import numpy as np

# Create a List with Some NaN values
sampleList = [11, 22, np.nan, 43, 23, np.nan, 35]
print("Original List:", sampleList)

# Create a NumPy Array
arr = np.array(sampleList)

# Return a list with non-NaN values
sampleList = list(map(int, arr[~np.isnan(arr)]))

print("List after removing NaN values:", sampleList)

Output

Original List: [11, 22, nan, 43, 23, nan, 35]
List after removing NaN values: [11, 22, 43, 23, 35]

Summary

Today, we learned about remove nan values from a Python list.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top