The question has changed, so too has the answer:

Strings can't be tested using math.isnan as this expects a float argument. In your countries list, you have floats and strings.

In your case the following should suffice:

cleanedList = [x for x in countries if str(x) != 'nan']

Old answer

In your countries list, the literal 'nan' is a string not the Python float nan which is equivalent to:

float('NaN')

In your case the following should suffice:

cleanedList = [x for x in countries if x != 'nan']
Answer from user764357 on Stack Overflow
🌐
Python Pool
pythonpool.com › home › tutorials › remove nan from a python list: math.isnan, numpy, and pandas
Remove NaN from a Python List: math.isnan, NumPy, and pandas
July 13, 2026 - 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.
Discussions

python - Removing a nan from a list - Stack Overflow
If you arrived at this thread for removing NaNs from a Python list (not pandas dataframes), the easiest way is a list comprehension that filters out NaNs. More on stackoverflow.com
🌐 stackoverflow.com
pandas - Remove NaN from lists in python - Stack Overflow
I have converted data frame rows to lists and in those list there are NaN values which I would like to remove. This is my attempt but the NaN values are not removed import pandas as pd df = pd. More on stackoverflow.com
🌐 stackoverflow.com
pandas - Removing nan from list - Python - Stack Overflow
I am trying to remove 'nan' from a list, but it is refusing to go. More on stackoverflow.com
🌐 stackoverflow.com
python - delete all nan values from list in pandas dataframe - Stack Overflow
if any elements are there along with nan then i want to keep element and want to delete nan only like example 1 -> index values 0 [nan,'a',nan,nan] output should be like index values ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

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.
🌐
pythonpool.com
pythonpool.com › home › tutorials › remove nan from a python list: math.isnan, numpy, and pandas
Remove NaN from a Python List: math.isnan, NumPy, and pandas
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.
🌐
pythonpool.com
pythonpool.com › home › tutorials › remove nan from a python list: math.isnan, numpy, and pandas
Remove NaN from a Python List: math.isnan, NumPy, and pandas
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.
🌐
pythonpool.com
pythonpool.com › home › tutorials › remove nan from a python list: math.isnan, numpy, and pandas
Remove NaN from a Python List: math.isnan, NumPy, and pandas
🌐
Medium
medium.com › @ryan_forrester_ › remove-nan-from-lists-in-python-a-complete-guide-d6656077f664
Remove NaN from Lists in Python: A Complete Guide | by ryan | Medium
January 7, 2025 - This recursive function: 1. Checks if each item is itself a list 2. If it is a list, processes each item inside it 3. If it’s not a list, checks if it’s NaN 4. Builds a new clean list with all NaN values removed · Time series data (data points over time) needs special handling because the order and spacing of data points matter. Here’s how to handle it properly: import pandas as pd import numpy as np from datetime import datetime, timedelta # Create realistic time series data with some missing values dates = pd.date_range(start='2024-01-01', periods=10, freq='H') temperatures = [23.5, n
🌐
Python Guides
pythonguides.com › remove-nan-values-from-a-list-in-python
How To Remove NaN Values From A List In Python
September 1, 2025 - Here, I’ve covered all the different ways to remove the NaN value from the list in Python using for loop, list comprehension, filter method, isnan() method from math module, isnan() method from numpy, and dropna() method from Pandas.
🌐
CodeGym
codegym.cc › java blog › learning python › remove nan using python
Remove NaN Using Python
August 14, 2024 - We first convert the list to a pandas Series, which is a one-dimensional array-like object. The dropna() function removes all NaN values from the Series.
Find elsewhere
🌐
Java2Blog
java2blog.com › home › python › python list › remove nan from list in python?
How to remove nan from list in Python? (with example) - Java2Blog
November 16, 2021 - Instead of using a for loop, you can use the list comprehension and pandas.isnull() function to remove a nan value from list in python as follows.
🌐
Delft Stack
delftstack.com › home › howto › python › remove nan from list python
How to Remove NaN From List in Python | Delft Stack
February 2, 2024 - Now, use list comprehension with the pandas.isnull() method to create a new list that excludes the NaN values. This method effectively identifies and removes NaN values, making it suitable for handling diverse data types:
🌐
Tutor Python
tutorpython.com › remove-nan-from-list-python
4 simple ways to Remove NaN from List in Python - Tutor Python
November 27, 2023 - Removing “NaN” values from a list in Python is a common task when working with data. Depending on your specific use case and data structure, you can choose the method that best suits your needs. Whether it’s using list comprehensions, the filter() function, pandas, or numpy, Python offers ...
🌐
Saturn Cloud
saturncloud.io › blog › python-pandas-how-to-remove-nan-and-inf-values
Python Pandas: How to remove nan and inf values | Saturn Cloud Blog
May 1, 2026 - The dropna() method removes rows or columns with NaN or -inf values from your data. By default, it removes all rows with at least one NaN or -inf value. You can specify the axis parameter to remove columns instead of rows. import pandas as pd ...
🌐
Reddit
reddit.com › r/learnpython › how to ignore or remove nan values from this pandas dataframe for loop?
r/learnpython on Reddit: How to ignore or remove nan values from this pandas dataframe for loop?
October 12, 2022 -

The existing dataframe is made up from different length rows, as a result I'm getting nan values called in my function, trying to stop that from happening if anyone can please explain or help with dropping/skipping the nan values, would be much appreciated, cheers.

code:

import webbrowser
from pandas import *
import numpy as np

data= read_csv(r"C:\Users\win95\Desktop\shop-list.csv")
itm = data['meat'].tolist()

url = "https://shop.coles.com.au/a/whiteman-edge/everything/search"

for t in itm:
    webbrowser.open(f"{url}/{t}?orderBy=UP&personaliseSort=false&pageNumber=1")
🌐
Altcademy
altcademy.com › blog › how-to-drop-nan-values-in-pandas
How to drop nan values in Pandas - Altcademy.com
January 11, 2024 - Removing NaN values simplifies the dataset and can make your analysis more straightforward. Pandas provides a powerful method called dropna() to deal with missing values.
🌐
LernerPython
lernerpython.com › home › lernerpython blog › pandas dropna: the best way to remove nan values from a series
Pandas dropna: The best way to remove NaN values from a series - LernerPython
September 25, 2026 - Coming to Python Pandas from NumPy? You’ll reach for np.isnan: Unfortunately, this works. Better, use s.isna (or s.isnull). But the best way to drop NaN? Use dropna:
🌐
Medium
koshurai.medium.com › nan-removal-with-python-3d97b954d16d
NAN Removal with Python. Removing NaN values from a list in… | by KoshurAI | Medium
January 15, 2023 - In conclusion, removing NaN values from a list in Python can be done using list comprehension with a conditional statement and the built-in isnan() function from the math library.
🌐
Reddit
reddit.com › r/learnpython › how to remove nan and .0 in pandas when importing lists from excel
r/learnpython on Reddit: How to remove nan and .0 in pandas when importing lists from excel
January 14, 2022 -

My excel spreadsheet:

model 1 model 2 model 3
36325677 11508380 11508589
36325678 11508451 11508572
36325673 11508422 11508541
36105349 11508486
36325679 11508492
11508385
11508470
11508416

My code:

import pandas as pd

dictionary = pd.read_excel('dictionary.xlsx').to_dict('list')

model_name = input('model name ')

print(dictionary[model_name])

Output when input = model 3:

[11508589.0, 11508572.0, 11508541.0, nan, nan, nan, nan, nan]

Desired output:

[11508589, 11508572, 11508541]