Native Python function to remove NoneType elements from list?

后端 未结 5 1779
终归单人心
终归单人心 2020-12-30 20:42

I\'m using Beautiful Soup in Python to scrape some data from HTML files. In some cases, Beautiful Soup returns lists that contain both string and NoneType

相关标签:
5条回答
  • 2020-12-30 20:52

    I think the cleanest way to do this would be:

    #lis = some list with NoneType's
    filter(None, lis)
    
    0 讨论(0)
  • 2020-12-30 20:53

    You could easily remove all NoneType objects from a list using a list comprehension:

    lis = [i for i in lis if i is not None]
    
    0 讨论(0)
  • 2020-12-30 20:54

    You can do this using list comprehension:

    clean = [x for x in lis if x != None]
    

    As pointed in the comments you could also use is not, even if it essentially compiles to the same bytecode:

    clean = [x for x in lis if x is not None]
    

    You could also used filter (note: this will also filter empty strings, if you want more control over what you filter you can pass a function instead of None):

    clean = filter(None, lis)
    

    There is always the itertools approach if you want more efficient looping, but these basic approaches should work for most day to day cases.

    0 讨论(0)
  • 2020-12-30 20:58

    List comprehension, as other answers proposed or, for the sake of completeness:

    clean = filter(lambda x: x is not None, lis)
    

    If the list is huge, an iterator approach is superior:

    from itertools import ifilter
    clean = ifilter(lambda x: x is not None, lis)
    
    0 讨论(0)
  • 2020-12-30 21:03

    For those who came here from Google

    In Python3 you can implement this using .__ne__ dunder method (or 'magic method' if you will):

    >>> list1 = [0, 'foo', '', 512, None, 0, 'bar']
    >>> list(filter(None.__ne__, list1))
    [0, 'foo', '', 512, 0, 'bar']
    

    This is how it works:

    • None.__ne__(None) --> False

    • None.__ne__(anything) --> NotImplemented

    NotImplemented exeption effectively is True, e.g.:

    >>> bool(None.__ne__('Something'))
    True
    

    As of the beginning of 2019, Python has no built-in function for filtering None values which avoids common pitfals with deleting zeroes, empty strings, etc.

    0 讨论(0)
提交回复
热议问题