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

Source: Internet
Author: User
Tags iterable

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

This function is used to traverse all elements from an iteration object iterable. When each element is run on the function object as a parameter, the elements that are judged to be True are retained, while those that are False are skipped, that is, to filter unnecessary elements. The iterable parameter is an iteratable object, such as a list, Dictionary, string, or function object with an iterator. The Parameter function is a function that can input elements for judgment and return values. If this parameter is null, the identity function is used by default as the default function.

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

Item for item in iterable if function (item ))

When the 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 result is as follows:

[1, 2, 3]

[6, 7]



Cai junsheng QQ: 9073204 Shenzhen

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.