Python filter() Function

In python filter() is an inbuilt function that returns an iterator where the items are filtered through a function to test if the item is accepted or not.

Syntax

filter(functioniterable)

Example to filter the array, and return a new array with only the values equal to or above 33

marks = [50, 26, 67, 18, 24, 86]

def myFunc(x):
  if x < 33:
    return False
  else:
    return True

passing_marks = filter(myFunc, marks)

for x in passing_marks:
  print(x)
Tutorials Class - Output Window

50
67
86


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.