23.3 Filter
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period2 exercises2 graded tests
23.3 Filter
Now consider another common pattern: going through a list and keeping only those items that meet certain criteria. This is called a filter.
def keep_evens(nums):
new_list = []
for num in nums:
if num % 2 == 0:
new_list.append(num)
return new_list
print(keep_evens([3, 4, 6, 7, 0, 1]))
Again, this pattern of computation is so common that Python offers a more compact and general way to do it, the filter
function. filter takes two arguments, a function and a sequence. The function takes one item and return True if the
item should. It is automatically called for each item in the sequence. You don't have to initialize an accumulator or
iterate with a for loop.
def keep_evens(nums):
new_seq = filter(lambda num: num % 2 == 0, nums)
return list(new_seq)
print(keep_evens([3, 4, 6, 7, 0, 1]))
Check Your Understanding
Exercise 1
1. Write code to assign to the variable filter_testing all the elements in lst_check that have a "w" in them using filter.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
2. Using filter, filter lst so that it only contains words containing the letter "o". Assign to variable lst2. Do not hardcode this.
Open the
02folder and editmain.py. Run Tests checks it.
For teachers
23.3 Filter
Students read Foundations of Python Programming's “Filter” in CS Room and work through its 2 Python coding exercises (2 graded).
- Time: 1 class period
- Tests: 2 in 2 graded exercises, set up for you when you import the lesson
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 1 |