The filter () function filters the sequence, filters out elements that are not eligible, returns an iterator object , and, if you want to convert to a list, you can use list () to convert.
The two parameters are received, the first is a function, the second is a sequence, each element of the sequence is passed as a parameter to the function, and then returns True or False, and finally the element that returns true is placed in the new list.
Filter(function, iterable)
Parameters
- Function-------judging functions.
- Iterable--Can iterate over objects.
return value
Returns an Iterator object
# 2. Use the filter function to process the list of numbers and filter out all the even number in the list num = [1,3,5,6,7,8]def Choice (x): if x%2 = = 0 :return= filter (choice,num)print(list ( ret))print(list (filter (lambda x:x%2==0,num)))
Python filter () function