Computer Science
Grade 9
20 min
Functional Programming: Filter and Reduce
Explore `filter` and `reduce` for data manipulation using functional programming paradigms.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define functional programming and its core idea of using pure functions.
Explain the purpose of the `filter()` function for selecting data.
Use `filter()` with a lambda function to create a new list based on a specific condition.
Explain the purpose of the `reduce()` function for aggregating data.
Use `reduce()` with a lambda function to compute a single value from a list.
Combine `filter()` and `reduce()` to solve multi-step data processing problems.
Differentiate between using `filter()` and a traditional `for` loop with an `if` statement.
Ever wanted to pick out all the high scores from a list or find the total of your shopping cart with just one line of code? 🛒🎮
Welcome to the world of functional programming! In this lesson, you'll learn abo...
2
Key Concepts & Vocabulary
TermDefinitionExample
Functional ProgrammingA style of programming where you build programs by applying and composing functions. It avoids changing data and focuses on creating predictable code by treating functions like mathematical equations.Instead of a loop that modifies a variable, you'd use functions like `filter` or `reduce` that take a list and produce a new result without changing the original list.
Higher-Order FunctionA special type of function that can take another function as an argument or that returns a function as its result.`filter()` is a higher-order function because you give it another function (the condition) to do its job.
filter()A built-in Python function that creates a new collection of items by 'filtering' an existing one. It keeps only the items f...
3
Core Syntax & Patterns
filter() Syntax
list(filter(condition_function, iterable))
Use this pattern to create a new list containing only the elements from the `iterable` (like a list) that make the `condition_function` true. We wrap it in `list()` because `filter` itself doesn't return a list directly.
reduce() Syntax
from functools import reduce
reduce(combining_function, iterable)
Use this pattern to boil down an `iterable` to a single value. The `combining_function` must take two arguments: the accumulated result so far and the next item from the list. Remember to import it first!
Lambda Function Pattern
lambda arguments: expression
This is a shortcut for creating a simple function. Use it inside `filter()` or `reduce()` to define your logic on the fly. The `expression` is what the...
5 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
Which `reduce` expression can be used to find the largest number in a list `nums`?
A.reduce(lambda a, b: a if a > b else b, nums)
B.reduce(lambda a, b: b, nums)
C.reduce(lambda a: max(a), nums)
D.reduce(lambda a, b: a > b, nums)
Challenging
You have a list of player data: `players = [{'name': 'Ryu', 'level': 99}, {'name': 'Ken', 'level': 55}, {'name': 'Zangief', 'level': 99}]`. How would you find the total level of all players who are level 99?
A.from functools import reduce
hi_levels = filter(lambda p: p['level'] == 99, players)
reduce(lambda a, b: a + b['level'], hi_levels, 0)
B.from functools import reduce
hi_levels = list(filter(lambda p: p['level'] == 99, players))
reduce(lambda a, b: a + b['level'], hi_levels)
C.from functools import reduce
hi_levels = list(filter(lambda p: p['level'] == 99, players))
levels = [p['level'] for p in hi_levels]
reduce(lambda a, b: a + b, levels)
D.reduce(lambda a, b: a['level'] + b['level'], players)
Challenging
Given `words = ['functional', 'is', 'fun']`, how can you use `reduce` to produce the string 'functional is fun'?
A.reduce(lambda a, b: a + b, words)
B.reduce(lambda a, b: a + ' ' + b, words)
C.reduce(lambda a: a + ' ', words)
D.list(reduce(lambda a, b: a + ' ' + b, words))
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free