✏️ Explanatory Question

What is the purpose of the filter() function in Python?

👁 279 Views
📘 Detailed Answer
279
Total Views
7
Related Qs
0%
Progress
💡

Answer with Explanation

The filter() function in Python is used to filter a sequence (list, tuple, etc.) by applying a test function to each element of the sequence. The test function should return a boolean value, indicating whether or not the element should be included in the filtered sequence. The filtered sequence is returned as a new sequence (a filter object) containing only the elements for which the test function returned True.

Syntax: filter(function, iterable)

where,

  • function is the test function to be applied to each element of the iterable
  • iterable is the sequence to be filtered (list, tuple, etc.)