grades = [
    70, 
    55.5, 
    85
]filter()
    Our first dip into the FP swimming pool…
  
filter
filter (predicate, list)
Returns new list, of the same type as the list being filtered, with possibly fewer items.
| Details | |
|---|---|
| predicate | lambda for True or False evaluation on each item | 
| list | List to filter | 
My typical examples…
passing_grades = filter(
    lambda x: x >= 70, 
    grades,
)print(
    passing_grades
)<filter object>Oops, those don’t look like grades : - O
print(
    list(
        passing_grades
    )
)[70, 85]tsteelematc: How about a cell that creates passing_grades_02 using a list comprehension
GitHub Copilot: I’ll add a new cell that creates passing_grades_02 using a list comprehension to filter the grades.
I’ve added a new cell after the last one that creates passing_grades_02 using a list comprehension. This cell:
- Uses list comprehension syntax [grade for grade in grades if grade >= 70]to filter grades that are 70 or above
- Stores the result in passing_grades_02
- Prints the result to show the filtered grades
The list comprehension is a more Pythonic and concise way to filter a list compared to using the filter() function, and it directly returns a list rather than a filter object.
Made changes.
passing_grades_02 = [grade for grade in grades if grade >= 70]
print(passing_grades_02)[70, 85]idiot-or-genius
But I like the way I can copy/paste Copilot MD into a notebook…
“modern” literate programming by Tom Steele : - )