Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created January 18, 2012 12:32
Show Gist options
  • Select an option

  • Save igniteflow/1632798 to your computer and use it in GitHub Desktop.

Select an option

Save igniteflow/1632798 to your computer and use it in GitHub Desktop.
Remove None elements from a list in Python
def remove_none_elements_from_list(list):
return [e for e in list if e != None]
@kkappler
Copy link

kkappler commented Feb 9, 2018

This worked for me but I had to replace != with 'is not', i.e.:
[e for e in list if e is not None]
otherwise I got TypeError: Could not compare [None] with block values

@ImadYIdrissi
Copy link

Better use this :

import pandas as pd
def remove_none_elements_from_list(list):
     return [e for e in list if(pd.notnull(e))]`

Why use pandas? Because it takes into account None and float("nan").

@Karrenbelt
Copy link

and don't use built-ins as variable names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment