Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Find the First Non-NaN Value in a Python List

  • 21 Jul, 2025
  • Com 0
First Non-NaN Value in Python List

Use is.nan() function with an if not statement in Python to find the first non-nan value in a list.

The is.nan() function helps us detect NaN values and then negate them using an if-not statement, filtering out all NaN values and returning only the values we need, which is the first non-NaN value we are looking for.

Finding the First Non-NaN Values in a Python List

You can represent the NaN value using float(‘nan’) or np.nan (If you are using the Numpy library.)

import math


def first_non_nan(main_list):
    for element in main_list:
        if not math.isnan(element):
            return element
    return None


main_list = [float('nan'), float('nan'), 21.0, 42.0, float('nan')]
first_non_nan = first_non_nan(main_list)

print(first_non_nan)

# Output: 21.0

In the above code, our input list contains a total of three NaNs. The first two NaNs are at positions 0 and 1.

Then, a non-nan element appears, which is 21.0. So, logically, the answer should be 21.0 since it is the first non-nan element. As soon as it finds the non-nan element, it breaks the loop and returns that first value.

If there are no non-NaN values (list with all the NaNs), it will return None because there is no other value other than NaN.

If you have a list with simple float values, you can use this approach.

Empty list

What if the input list is empty? How will you deal with it? Well, based on our code, it will return None because there is no non-NaN value.

import math


def first_non_nan(empty_list):
    for element in empty_list:
        if not math.isnan(element):
            return element
    return None


empty_list = []

first_non_nan = first_non_nan(empty_list)

print(first_non_nan)

# Output: None

List with All NaN Values

What if the list contains all the NaN values and not a single non-NaN value? Well, it will still return None.

import math


def first_non_nan(nan_list):
    for element in nan_list:
        if not math.isnan(element):
            return element
    return None


nan_list = [float('nan'), float('nan'), float('nan')]

first_non_nan = first_non_nan(nan_list)

print(first_non_nan)

# Output: None

Using NumPy (with np.isnan)

If your input data is a numpy array, you can use numpy.isnan() method to create a boolean array where True indicates NaN values.

Using ~np.isnan(), we can invert the mask to select non-NaN values.

Use array indexing to find the first element and then use the .any() method to check if there’s at least one non-NaN value to avoid index errors.

import numpy as np


def first_non_nan_numpy(np_array):
    mask = ~np.isnan(np_array)
    return np_array[mask][0] if mask.any() else None


np_array = np.array([np.nan, np.nan, 21.0, 42.0, np.nan, 84.0])

first_non_np_nan = first_non_nan_numpy(np_array)

print(first_non_np_nan)

# Output: 21.0

And we get the exact output, but this time it is from the array. Use NumPy for large numerical datasets or when working with arrays.

For mixed types, use type checking or try-except blocks to handle exceptions.

Post Views: 117
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Get the Last Element of a List in Python
How to Convert List to CSV in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend