🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Table of Contents
- Introduction
filter()Function Syntax- Understanding
filter() - Examples
- Filtering with a Function
- Using a Lambda Function
- Real-World Use Case
- Conclusion
Introduction
The filter() function allows you to create an iterator that extracts elements from an iterable (like a list or tuple) based on a function that evaluates each element. If the function returns True, the element is included in the iterator.
filter() Function Syntax
The syntax for the filter() function is as follows:
filter(function, iterable)
Parameters:
- function: A function that tests each element of the iterable. It should return
TrueorFalse. - iterable: The iterable to be filtered.
Returns:
- An iterator yielding those items of the iterable for which the function is
True.
Understanding filter()
The filter() function applies the function to each element in the iterable and returns an iterator with elements for which the function returns True. If None is passed as the function, it returns the elements that are true by default.
Examples
Filtering with a Function
To demonstrate the basic usage of filter(), we will filter out even numbers from a list using a defined function.
Example
def is_even(n):
return n % 2 == 0
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(is_even, numbers)
print("Even numbers:", list(even_numbers))
Output:
Even numbers: [2, 4, 6]
Using a Lambda Function
This example shows how to use a lambda function with filter() to filter out odd numbers from a list.
Example
numbers = [1, 2, 3, 4, 5, 6]
odd_numbers = filter(lambda n: n % 2 != 0, numbers)
print("Odd numbers:", list(odd_numbers))
Output:
Odd numbers: [1, 3, 5]
Real-World Use Case
Filtering User Input
In real-world applications, the filter() function can be used to filter user inputs based on specific criteria.
Example
users = [
{"name": "Raj", "age": 25},
{"name": "Sita", "age": 30},
{"name": "Mohan", "age": 20},
{"name": "Lakshmi", "age": 35}
]
# Filter users who are 30 years old or older
adults = filter(lambda user: user["age"] >= 30, users)
print("Adult users:", list(adults))
Output:
Adult users: [{'name': 'Sita', 'age': 30}, {'name': 'Lakshmi', 'age': 35}]
Filtering Sensor Data
Another real-world use case is filtering sensor data to remove invalid readings.
Example
sensor_data = [23.4, 25.1, None, 22.8, 24.7, None, 26.3]
# Filter out None values
valid_data = filter(lambda x: x is not None, sensor_data)
print("Valid sensor data:", list(valid_data))
Output:
Valid sensor data: [23.4, 25.1, 22.8, 24.7, 26.3]
Conclusion
The filter() function in Python is used for creating an iterator from elements of an iterable for which a function returns true. By using this function, you can easily filter data based on specific criteria, making it particularly helpful in scenarios such as data validation, user input filtering, and more. This function enhances the flexibility and efficiency of data processing in Python.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment