Filter() Function

Subscribe to Tech With Tim!

Filter Function

The filter function is similar to the map function. They both make use of a function and and a list. The filter function will pass every element of the given list into a function that returns a Boolean value. If the function returns True for a given element that element will be added to the new returned list. Otherwise it will not.

def isAOne(x):
    return x == 1

nums = [1,1,6,7,8,0,1,1]

newList = list(filter(isAOne,nums))

# newList is [1,1,1,1]