Learning

Functional Programming Tools

Functional Programming Tools

map() — Transform

python
# Apply function to every element nums = [1, 2, 3, 4, 5] # With named function def square(n): return n ** 2 squares = list(map(square, nums)) # [1, 4, 9, 16, 25] # With lambda (more common) squares = list(map(lambda x: x**2, nums)) # Multiple iterables names = ['alice', 'bob'] upper = list(map(str.upper, names)) # ['ALICE', 'BOB'] a = [1, 2, 3] b = [10, 20, 30] summed = list(map(lambda x, y: x + y, a, b)) # [11, 22, 33] # map vs comprehension (comprehension is often preferred) squares = [x**2 for x in nums] # More Pythonic!

filter() — Select

python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Keep only evens evens = list(filter(lambda x: x % 2 == 0, nums)) # [2, 4, 6, 8, 10] # With named function def is_prime(n): if n < 2: return False return all(n % i != 0 for i in range(2, int(n**0.5) + 1)) primes = list(filter(is_prime, range(20))) # [2, 3, 5, 7, 11, 13, 17, 19] # Remove falsy values data = [0, 1, '', 'hello', None, [], [1, 2]] truthy = list(filter(None, data)) # [1, 'hello', [1, 2]] # filter vs comprehension evens = [x for x in nums if x % 2 == 0] # More Pythonic!

reduce() — Accumulate

python
from functools import reduce nums = [1, 2, 3, 4, 5] # Sum all numbers # Step 1: 1+2=3, Step 2: 3+3=6, Step 3: 6+4=10, Step 4: 10+5=15 total = reduce(lambda acc, x: acc + x, nums) # 15 # Find maximum maximum = reduce(lambda acc, x: acc if acc > x else x, nums) # 5 # With initializer (3rd arg) total = reduce(lambda acc, x: acc + x, nums, 100) # 115 (starts with 100 instead of first element) # Flatten a list of lists nested = [[1, 2], [3, 4], [5]] flat = reduce(lambda acc, x: acc + x, nested, []) # [1, 2, 3, 4, 5]

Chaining Operations

python
from functools import reduce nums = range(1, 11) # Pipeline: filter evens → square → sum result = reduce( lambda acc, x: acc + x, map(lambda x: x**2, filter(lambda x: x % 2 == 0, nums) ) ) # (2^2 + 4^2 + 6^2 + 8^2 + 10^2) = 220 # Comprehension equivalent (much more readable!) result = sum(x**2 for x in nums if x % 2 == 0)
Key Rules
  • •map() transforms every element — returns a map object, wrap in list() to view results
  • •filter() keeps elements where function returns True — returns a filter object
  • •reduce() collapses iterable into a single value — MUST import from functools
  • •Comprehensions are usually more readable than map/filter chains in Python
  • •reduce() with an initializer handles empty iterables safely (returns initializer)
  • •filter(None, iterable) is a quick way to remove all falsy values (0, '', None, [], False)
Your Task

Create a function `process_data(numbers)` that takes a list of numbers and returns a dictionary: `'positives'` (count of positive nums using filter), `'squared'` (list of squared nums using map), and `'product'` (product of all nums using reduce, return 0 if empty). Import reduce from functools.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should import reduce from functools
Should define process_data function
Should use filter for positives
Should use map for squared
Should use reduce for product
Should handle empty list edge case