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, 1382 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.)
Nice, easy and short thanks man... Hopefully you give more example tho... I mean real life hacks.
Rs
* Thankful to Allah *
Kurdy
(05-20-2022, 01:01 PM)Mr.Kurd Wrote: Nice, easy and short thanks man... Hopefully you give more example tho... I mean real life hacks.

Thank you very much kurd. I'll try to give more real life example codes.
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,246 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,636 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,669 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,843 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,607 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,841 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,792 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,829 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,825 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,827 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)