Skip to content
Advertisement

Remove NaN in a python list using max

In a recent interview I was given the following problem.

We have a list l = [NaN, 5, -12, NaN, 9, 0] and we want to replaceNaN with -9 using the max function knowing that max(NaN, -9) = -9. What I have tried is :

JavaScript

but the output is still the same list. Can’t figure out how to code this.

Advertisement

Answer

You can use nan_to_num function to change the NaN value to any number

JavaScript

OUTPUT [-9.0, 5, -12, -9.0, 9, 0]

Here you got -9.0 instead of -9 (I think because NaN Type is float).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement