[PYTHON] Operation of filter (None, list)

A memo when you are worried about the operation of the built-in function filter

I thought that filter is a high-rise function and takes a function as an argument, but apparently it can take None as an argument.

Anxious behavior

>>> a = [0,1,1.1,'a',None,False]
>>> b = filter(None,a)
>>> list(b)
[1, 1.1, 'a']

As for the operation, it seems that 0, None, False which are False in bool are removed.

>>> b = filter(lambda x:bool(x),a)
>>> list(b)
[1, 1.1, 'a']

Solution

Confirmed using help

help(filter)

class filter(object)
 |  filter(function or None, iterable) --> filter object
 |
 |  Return an iterator yielding those items of iterable for which function(item)
 |  is true. If function is None, return the items that are true.
 |
 |  Methods defined here:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __next__(self, /)
 |      Implement next(self).
 |
 |  __reduce__(...)
 |      Return state information for pickling.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.

When None comes to the function part, it seems to behave like filter (lambda x: x, list).

bonus

When I tried to check it from the source code, it was quite difficult.

Checking the source using inspect cannot be done with the builtin function.

>>> import inspect
>>> inspect.getsource(filter)
...
TypeError: <class 'filter'> is a built-in class

I checked it from Documentation on Github, but the source is not included. That should be it, the built-in functions are written in C. You can check the source code of the filter function as of Python2 with builtin_filter in Mercurial. I wonder if this hasn't changed even after becoming Python3 ...?

Recommended Posts

Operation of filter (None, list)
List reverse operation
Basic operation list of Python3 list, tuple, dictionary, set
[Python] Operation of enumerate
List of python modules
Copy of multiple List
Basic operation of pandas
Filter List in Python
Basic operation of Pandas
List of activation functions (2020)
List operation memo writing
Depth of nested list
[For beginners] Simple sample of list operation functions map, reduce, filter for each language
Display of fractions (list)
EP 7 Use List Comprehensions Instead of map and filter
Summary of Python3 list operations
List of nodes in diagrams
List of self-made Docker images
Multidimensional array initialization of list
[Python] Copy of multidimensional list
Filter the output of tracemalloc
List of useful coding styles
[Python] List of major destructive methods of list operation (addition / deletion / sorting)
[Python] Operation memo of pandas DataFrame
Judgment of if by list comprehension
List of ready-to-use word embed vectors
List of packages installed by conda
Notify LINE of train operation information
List of frequently used Linux commands
Operation memo of Conda virtual environment
Generate a list of consecutive characters
About the basics list of Python basics
Note: List of customized special names
Effective Python memo Item 7 Use list comprehension instead of map and filter