Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.7: Filter Function with example

2 Replies, 1437 Views

[Image: BFMMlbcQvml9HSqXcvNp]

Filters In Python:
Filter function filters the iterables means it will return those items on which the given condition is true

Code:
#program to print each even number of list
mylist = [1,2,3,4,5,6,7,8,9]
mylist = list(filter(lambda x: x%2==0, mylist))
print(mylist)


It is not necessary to use lambda functions; you can also create your own functions and call them as  an argument in the filter function just like this.

Code:
mylist = [1,2,3,4,5,6,7,8,9]
def greaterThan(num):
    return num > 5
list_2 = list(filter(greaterThan,mylist))
print(list_2)


But using lambda functions make it easy to write code and readable.

Exclamation Note: We use filters with iterables like list, tuple etc..
Love me like you do Heart
(This post was last modified: 05-18-2022, 06:39 AM by Covid-19.)

Messages In This Thread
Python 2.7: Filter Function with example - by Covid-19 - 05-17-2022, 03:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,290 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,722 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,761 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,891 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,648 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,881 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,831 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,897 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,863 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,892 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)