Python standard library: Built-in functions filter (function, iterable)

Source: Internet
Author: User
Tags iterable

This function is used to traverse all elements from an iterative object iterable , and evaluates to True When each element is run as a parameter to a function object. element is preserved, while the element that is False is skipped, which is the goal of filtering unwanted elements. The parameter iterable is an iterative object, such as a list, dictionary, string, or function object with an iterator. Parameter functions are a function that can input elements into the judgment and return values, and if this argument is null, the identity function identity is used by default as a default function.

When function is non-null, it is equivalent to generating an expression:

Item for item in Iterable if function (item))

When function is empty, it is equivalent to generating an expression:

Item for item in Iterable if item

Example:

#filter () L = List (filter (None, [0, 1, 2, 3]) print (l) A = [1,2,3,4,5,6,7]b = List (filter (lambda x:x > 5, a)) print (b )


The output results are as follows:

[1, 2, 3]

[6, 7]



Cai Junsheng qq:9073204 Shenzhen

Python standard library: Built-in function filter (function, iterable)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.