filter() function in Python
This article is about using filter() with lambda.
filter() function :
This filter() function is used to filter some content from a given sequence where the sequence may be a list, tuple or string etc.
Syntax : filter(function, iterable)
where,
- function refers to a function that accepts an argument and returns bool i.e True or False based on some condition.
- iterable refers to the sequence which will be filtered.
How it works ?
Actually filter() function iterates over all elemnts of the sequence and for each element the given callback function is called.
- If False is returned by the function then that element is skipped.
- If True is returned by the function then that element is added into a new list.
Filter a list of strings in Python using filter() :
#Program :
# A list containg string as elemnt
words = ['ant', 'bat', 'dog', 'eye', 'ink', 'job']
# function that filters words that begin with vowel
def filter_vowels(words):
vowels = ['ant', 'eye', 'ink']
if(words in vowels):
return True
else:
return False
filtered_vowels = filter(filter_vowels, words)
# Print words that start with vowel
print('The filtered words are:')
for vowel in filtered_vowels:
print(vowel)Output : ant eye ink
In the above example we had created one separate function as filtered_vowels and we passed it to filter() function.
Using filter() with Lambda function :
In this we will pass an lambda function rather passing a separate function in filter() function. And the condition is to select words whose length is 6.
So, let’s see an example of it.
#Program :
# A list containg string as elemnt
words = ['ant', 'basket', 'dog', 'table', 'ink', 'school']
#It will return string whose length is 6
filtered_vowels = list(filter(lambda x : len(x) == 6 , words))
# Print words that start with vowel
print('The filtered words are:')
for vowel in filtered_vowels:
print(vowel)Output : The filtered words are: basket school
Filter characters from a string in Python using filter() :
Now, let’s take example how to filter a character in a string and remove that character in the string.
#Program :
# A list containg string as elemnt
str_sample = "Hello, you are studying from Btech Geeks."
#It will return particular character in the string
#then those chharacters are removed from the string
filteredChars = ''.join((filter(lambda x: x not in ['e', 's'], str_sample)))
# Print the new string
print('Filtered Characters : ', filteredChars)Output : Hllo, you ar tudying from Btch Gk.
Filter an array in Python using filter() :
Suppose we have an array. So, let’s see it how we can filter the elements from an array.
#Program :
# Two arrays
sample_array1 = [1,3,4,5,21,33,45,66,77,88,99,5,3,32,55,66,77,22,3,4,5]
sample_array2 = [5,3,66]
#It will return a new array
#It will filter
#if array2 elemnts are present in array1 then that element will not be removed
filtered_Array = list(filter(lambda x : x not in sample_array2, sample_array1))
print('Filtered Array : ', filtered_Array)Output : [1, 4, 21, 33, 45, 77, 88, 99, 32, 55, 77, 22 ,4]