[Tutor] filter() builtin function
Manprit Singh
manpritsinghece at gmail.com
Thu Sep 30 04:59:59 EDT 2021
Dear sir,
For the filter function in Python documentation , a line is written as
below:
Construct an iterator from those elements of *iterable* for which *function*
returns true.
*What does that true means - Boolen value True or true object - for
example 1*
*Like if I need only odd numbers from a list , what will be the correct
lambda function to be placed inside the filter function ?*
*lambda x: x%2 (That will return 1 for odd numbers which is a true
object)*
*or *
*lambda x : x%2 == 1 ( Will return Boolean True for odd numbers)*
*seq = [2, 3, 6, 9, 0, 7]list(filter(lambda x: x%2, seq)) gives the answer
= *[3, 9, 7]
seq = [2, 3, 6, 9, 0, 7]
list(filter(lambda x: x%2 == 1, seq)) also gives the same answer = [3, 9,
7]
Which one is the correct way to do the task solved just above in 2 ways ?
Regards
Manprit Singh
More information about the Tutor
mailing list