Created
January 18, 2012 12:32
-
-
Save igniteflow/1632798 to your computer and use it in GitHub Desktop.
Remove None elements from a list in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def remove_none_elements_from_list(list): | |
| return [e for e in list if e != None] |
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").
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
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