Higher Order Functions — Practice Questions
50 questions. Try each one yourself before checking the answer.
Write a lambda that doubles a number, assign it to double, and call it with 5.
Given numbers = [1, 2, 3, 4], use map() with a lambda to double every number, converting the result to a list.
Given numbers = [1, 2, 3, 4], use map() with a regular (non-lambda) function square(n) to square every number.
Given numbers = [1, 2, 3, 4, 5, 6, 7, 8], use filter() with a lambda to keep only the even numbers.
Given words = ["apple", "", "banana", "", "cherry"], use filter() with None as the function to drop all "falsy" (empty) values.
Given scores = [45, 60, 30, 85], print whether ANY score is below 40 using any().
Given scores = [45, 60, 30, 85], print whether ALL scores are at least 40 using all().
Given names = ["Alice", "Bob"] and ages = [30, 25], use zip() to pair them and print the resulting list of tuples.
Given fruits = ["apple", "banana", "cherry"], print each fruit with its index using enumerate().
Given words = ["banana", "kiwi", "apple", "fig"], sort them by length using sorted() with a lambda key.
Given list1 = [1, 2, 3] and list2 = [10, 20, 30], use map() with a lambda taking two arguments to add them pairwise.
Given words = ["cat", "elephant", "dog", "hippopotamus"], use filter() to keep only words longer than 4 characters.
Given people = [("Alice", 30), ("Bob", 25), ("Carol", 30)], sort by AGE, then by NAME (as a tiebreaker), using a tuple key.
Import reduce from functools and use it to compute the sum of [1, 2, 3, 4, 5] with a lambda.
Use reduce() with an explicit starting value of 100 to add up [1, 2, 3], showing the initial value gets included in the accumulation.
Given usernames = ["alice", "bob123", "carol"], use any() with a generator expression to check if any username contains a digit (via .isdigit() on each character).
Given passwords = ["abc12345", "xyz98765", "short"], use all() to check whether EVERY password is at least 8 characters long.
Given numbers = range(1, 11), chain filter() (keep evens) and map() (square them) together.
Given raw_scores = ["88", "92", "75"], use map() with int DIRECTLY (no lambda needed) to convert every value.
Given tasks = ["Write report", "Review PR", "Deploy"], print each task numbered starting from 1, using enumerate(tasks, start=1).
Given celsius_temps = [0, 20, 37, 100], use map() to convert every value to Fahrenheit.
Given emails = ["alice@test.com", "not-an-email", "bob@test.com"], use filter() to keep only the ones containing "@".
Use reduce() to compute the factorial of 6, folding a range of numbers together with multiplication.
Use reduce() to find the MAXIMUM value in [4, 9, 2, 7, 15, 3], without using the built-in max().
Given students = [{"name": "Alice", "score": 88}, {"name": "Bob", "score": 35}], use any() to check if ANY student failed (score < 40).
Using the same students list from Q25, use all() to check whether EVERY student passed.
Given employees = [{"name": "Alice", "salary": 60000}, {"name": "Bob", "salary": 75000}], sort by SALARY descending using sorted() with reverse=True.
Given names = ["Alice", "Bob", "Carol"] and emails = ["a@x.com", "b@x.com", "c@x.com"], use zip() to build a dictionary mapping name to email.
Given runners = ["Alice", "Bob", "Carol"] (already in finishing order), use enumerate() to print a numbered "podium" list starting from 1st place.
Given prices = [10.5, 45.0, 5.75], use map() to format each as currency, then join the results into one printable line.
Given numbers = range(1, 21), chain filter() (evens), map() (square), and reduce() (sum) into a single functional pipeline computing the total.
Use reduce() to build a running MAXIMUM history — not just the final max, but a LIST showing the max-so-far after each step.
Given products = [{"name": "Pen", "category": "Office", "price": 10}, {"name": "Laptop", "category": "Tech", "price": 999}, {"name": "Notebook", "category": "Office", "price": 45}], sort by category ascending, then price descending WITHIN each category.
Given orders = [{"id": 1, "items": []}, {"id": 2, "items": ["Pen"]}], use any() to find whether ANY order is empty, and all() to check whether EVERY order has at least one item.
Use reduce() to concatenate a list of words into one sentence, separated by spaces, WITHOUT using .join().
Given numbers = [1, 2, 3, 4], use map() with a lambda to produce (n, n**2) tuples, then "unzip" the result back into two separate lists using zip(*...).
Rewrite list(map(lambda n: n ** 2, filter(lambda n: n % 2 == 0, range(10)))) as an equivalent list comprehension, and comment on which reads more clearly.
Compute the sum of [1, 2, 3, 4, 5] THREE ways — a for loop, sum(), and reduce() — and confirm all three agree.
Given people = [("Alice", "B"), ("Bob", "A"), ("Carol", "B"), ("David", "A")], sort ONLY by grade using sorted(), and demonstrate that people with the SAME grade keep their original relative order (Python's sort is stable).
Given people = [("Alice", 30), ("Bob", 25)], sort using operator.itemgetter(1) instead of a lambda, and explain why this can be preferred.
Build a "Transaction Pipeline". Given a list of transaction dicts (amount, type), use filter() to keep only deposits, map() to extract amounts, and reduce()/sum() to compute the total deposited.
Build an "Employee Ranking System". Given a list of employee dicts, sort by department, then by salary descending, and print a formatted ranked list.
Build a "Grade Validator". Given a list of scores, use all() to check every score is in 0-100, and any() to check if any score is a perfect 100.
Build a "Text Processing Toolkit". Given a paragraph, use filter() to remove short words (<=2 chars), map() to capitalize the rest, and print the result joined back into a sentence.
Build a "Leaderboard Builder". Given a dict of player: score, sort by score descending, then use enumerate() to assign rank numbers, printing a formatted leaderboard.
Explain why many Python style guides prefer list comprehensions over map()/filter() with lambda, but note ONE case where map() with a NAMED function is arguably clearer. Demonstrate both.
Explain why reduce() was moved out of Python's builtins into functools in Python 3, and why an explicit loop or sum()/max() is often considered more readable than reduce() for simple accumulations.
Demonstrate that map() and filter() return LAZY iterator objects (not lists), tying this back to the Iterators & Generators module, and show that they can only be consumed once.
Explain when operator.itemgetter/operator.attrgetter are preferable to a lambda as a sorted() key, demonstrating attrgetter on a list of simple objects.
Build a "Functional Toolkit Mastery Report" — the most complete demonstration in this module. Given a list of student dicts (name, score), use: filter() + map() to build a list of passing names in uppercase, any()/all() for pass-rate checks, sorted() with a multi-key lambda to rank everyone, and enumerate() to number the final leaderboard.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session