Tech With Tim Logo
Go back

Filter() Function

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]
Design & Development by Ibezio Logo

Join Tim's Coding Corner Newsletter

Register and I'll give you an entire FREE guide on How To Make Money From Coding just to prove to you how much value is coming. Check your email for immediate access.

And if that wasn't enough... each email will contain free coding challenges, project ideas and software engineering insights and tips.

We won't send you spam, just free value. Unsubscribe at any time.