Thanks to visit codestin.com
Credit goes to www.tutorialsteacher.com

Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python - Filter Function

The filter() function calls the specified function which returns boolen for each item of the specified iterable (list).

Syntax:

filter(function, iterable) --> filter object

Parameters:

  1. function: The function to be called for each element of the specified iterable.
  2. iterables: One or more iterables separated by a comma (such as string, list, tuple, dictionary).

Return Value:

Returns an iterator object of the filter class.

The filter() function also receives two arguments, a function and a sequence (e.g. a list). Each item in the list is processed by the function which returns True or False. Only those items which return True are stored in a filter object. This can then be conveniently converted into a sequence.

The following function is_even() returns True if the passed number is an even number, otherwise it returns False. This function is used inside filter() along with the list object.

def is_even(x):
	if x%2 == 0:
		return True
	else:
		return False
Example: filter()
>>> result = filter(lambda x: x%2==0, numbers)
>>> next(result)
2
>>> next(result)
4

You can specify None as the function argument. In this case, the filter() function will return all truthy values from an iterable, as shown below.

Example: filter() with Lambda Function
>>> mylist = [0, 1, 2, 3, False, True, 5]
>>> result = filter(None, mylist)
>>> list(result)
[1, 2, 3, True, 5]
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.