Important Notice:

Course Content
The Calendar Module
Library Function/Predefined Functions
0/4
Sys Module
Library Function/Predefined Functions
0/2
OS Module
Library Function/Predefined Functions
0/2
Functions in Python (Chapter-10) M3-R5
About Lesson

filter() Function 

                                        Python में filter एक ऐसा function है जो logic के आधार पर किसी list में से data को filter करके extract करने के लिए प्रयोग किया जाता है। इस function में आप एक function और एक list को argument के रूप में pass करते है। List के सभी items के साथ logic perform किया जाता है जो item उस logic के अनुरूप होता है उसे store कर लिया जाता है। इस प्रकार वे सभी items जो उस logic को fulfil करते है उन्हें return कर दिया जाता है।

            The filter() function returns an iterator where the items are filtered through a function to test if the item is accepted or not.

 

 

filter() Function 

                In Python, filter is a function that is used to extract data from a list by filtering it based on logic. In this function, you pass a function and a list as arguments. The logic is performed with all the items in the list and the item that is in accordance with that logic is stored. Thus, all the items that fulfill that logic are returned.

              The filter() function returns an iterator where the items are filtered through a function to test if the item is accepted or not.

ages=[5,10,15,18,20,25,35]

def myfunc(x):

    if x<18:

        return False

    else:

        return True

adults=list(filter(myfunc,ages))

for x in adults:

    print(x)

Output

18 20 25 35

Using Filter Function with Lambda function

ages=[5,10,15,18,20,25,35]

adult=filter(lambda a:a>18,ages)

for x in adult:

    print(x)

Output

20 25 35

 

error: Content is protected !!