Sunday, 30 August 2026

๐Ÿš€ Day 97/150 – Filter() Function in Python

 

๐Ÿš€ Day 97/150 – filter() Function in Python

The filter() function is a built-in Python function used to select elements from an iterable based on a condition. It returns only those elements for which the given function evaluates to True.

Syntax:

filter(function, iterable)

In this post, we'll explore four common examples of using the filter() function in Python.


Method 1 – Using filter() with a Normal Function

Filter even numbers from a list using a normal function.

def is_even(num): return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6] result = list(filter(is_even, numbers)) print(result)







Output

[2, 4, 6]

Explanation

  • is_even() returns True if a number is even.

  • filter() applies this function to every element in the list.

  • Only the elements for which the function returns True are kept.

  • list() converts the filter object into a list.


Method 2 – Using filter() with a Lambda Function

Use a lambda function to write the filtering logic in a single line.

numbers = [10, 15, 20, 25, 30] result = list(filter(lambda x: x > 20, numbers)) print(result)





Output
[25, 30]


Explanation
  • lambda x: x > 20 checks whether each number is greater than 20.

  • filter() keeps only the numbers that satisfy the condition.

  • The result is converted into a list.


Method 3 – Filtering Strings

Filter words whose length is greater than 5.

words = ["Python", "Java", "Programming", "Code"] result = list(filter(lambda word: len(word) > 5, words)) print(result)









Output

['Python', 'Programming']

Explanation

  • len(word) > 5 checks the length of each word.

  • filter() keeps only the words with more than 5 characters.

  • This method is useful when working with text data.


Method 4 – Taking User Input

Filter even numbers entered by the user.

numbers = list(map(int, input("Enter numbers separated by spaces: ").split())) result = list(filter(lambda x: x % 2 == 0, numbers)) print("Even numbers:", result)







Sample Input
10 15 20 25 30

Output

Even numbers: [10, 20, 30]

Explanation

  • input() reads the numbers as a string.

  • split() separates them into a list.

  • map(int, ...) converts each value to an integer.

  • filter() selects only the even numbers.


Comparison of Methods

MethodBest For
Normal FunctionReusable filtering logic
Lambda FunctionShort and simple conditions
String FilteringFiltering text data
User InputInteractive programs

๐Ÿ”ฅ Key Takeaways

  • filter() selects elements that satisfy a condition.

  • It returns a filter object, which is usually converted to a list using list().

  • filter() works with both normal functions and lambda functions.

  • It is commonly used to filter numbers, strings, and other collections.

  • filter() makes code shorter and more readable than writing equivalent loops.

Stay tuned for Day 98 of the #150DaysOfPython series! ๐Ÿš€

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (353) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (96) Coursera (306) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (438) Data Strucures (20) Deep Learning (223) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (79) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (411) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1393) Python Coding Challenge (1282) Python Library (26) Python Mathematics (22) Python Mistakes (51) Python Pattern Challenge (29) python quixz (1) Python Quiz (665) Python Tips (114) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)