Higher Order Functions: Practice Questions
100 questions. Try each one yourself before checking the answer.
Short on time? Filter by Must Do for the 25 questions that cover this topic on their own.
Show that a function is a value: store it in a variable, put it in a list, and pass it to another function.
Write the same small function with def and with lambda, and show they behave identically.
Use map() to apply a function to every item, and show it returns an iterator.
Use map() with a named function and with a method.
Use map() with two iterables at once.
Use filter() to keep only the items that pass a test.
Use filter(None, ...) to drop every falsy value.
Use sorted(key=…) — the tool that replaces four earlier workarounds.
Sort a list of tuples by any field — retiring the Topic 9 trick.
Sort a dictionary by its values — retiring the Topic 11 trick.
Use .sort(key=…) to sort a list in place, and contrast it with sorted().
Use key= with max() and min().
Use any() to ask whether at least one item passes a test.
Use all() to ask whether every item passes a test.
Combine any() and all() for a validation check.
Use zip() to walk two lists together — retiring range(len(...)).
Use zip() to build a dictionary — retiring the Topic 18 workaround.
Use zip(*rows) to transpose — retiring the nested comprehension.
Use enumerate() to get the index and value together.
Combine enumerate() with zip().
Use functools.reduce() to fold a sequence into a single value.
Use reduce() with a starting value, and show when it genuinely helps.
Compare map() with the equivalent comprehension and say which reads better.
Compare filter() with the equivalent comprehension.
Store functions in a dictionary and pick one at runtime — the Topic 11 dispatch table, revisited.
Sort objects by an attribute — retiring the Topic 15 workaround.
Use operator.itemgetter and attrgetter instead of writing lambdas.
Sort by several fields in different directions.
Sort using a computed key, including a case-insensitive and a "last word" sort.
Use zip() for calculations across several sequences.
Use enumerate() to find positions and build numbered output.
Use any() and all() to replace flag-and-loop validation.
Use functools.partial to fix some arguments of a function in advance.
Write your first decorator — a function that wraps another function.
Use functools.wraps so a decorated function keeps its own name and docstring.
Write a timing decorator.
Write a decorator that takes arguments of its own.
Use functools.lru_cache — retiring the Topic 11 hand-written memoisation.
Use map() for type conversion across a whole dataset.
Chain filter, map and sorted together, then write the same thing as a comprehension.
Write functions that build other functions — the Topic 7 factory, put to work.
Sort nested data by a value buried inside it.
Use reduce() where it genuinely reads better than a loop.
Use zip() to compare, pair and rearrange data.
Write a decorator that validates its function's arguments.
Write a retry decorator — the Topic 13 pattern, packaged.
Use any() and all() with zip() to compare collections.
Write a function that takes a function as an argument and applies it in different ways.
Sort a Counter and a dictionary of lists by derived values.
Measure whether map is actually faster than a comprehension.
Build a leaderboard with ranks, ties handled and multiple sort criteria.
Build a small query engine over records using functions as filters.
Rank word frequencies using sorted(key=) on a Counter.
Process a CSV with map, filter and sorted together.
Group records and sort each group.
Build a top-N report with ties reported honestly.
Validate a batch of records with all() and report every failure.
Build a function pipeline that applies stages in order.
Combine several datasets with zip() and report on them.
Build a report with enumerate() supplying rank numbers throughout.
Build a decorator that logs every call to a file.
Use lru_cache to speed up a genuinely expensive calculation.
Sort and filter file data with key functions.
Build a flexible sorting menu where the user picks the field.
Analyse survey results with Counter, sorted and any/all.
Write a decorator that counts how often each function is called.
Merge and reconcile two datasets using zip, sorted and set operations.
Build a scoring system where the weights are supplied as functions.
Apply a chain of transformations chosen at runtime.
Build a small statistics toolkit where each metric is a function.
Show that Python's sort is stable, and use it to sort by two fields with different directions.
Show that key= is called exactly once per item, and why that matters.
Show that map and filter are lazy, and the bugs that causes.
Show that any() and all() short-circuit, and why that matters.
Explain what any() and all() return for an empty iterable.
Show that zip() silently stops at the shortest input, and how to avoid losing data.
Show the late-binding trap when lambdas are created inside a loop.
Show what a decorator breaks when it forgets functools.wraps.
Show where reduce() becomes unreadable, and what to use instead.
Show the three things that break lru_cache.
Show what goes wrong when a key function returns inconsistent types.
Show the cases where map and filter genuinely lose to a comprehension.
Show what happens when decorators are stacked, and in what order they run.
Compare key= with defining __lt__ on a class.
Show a chain of higher-order functions that has become unreadable, and fix it.
Build a Leaderboard System with pluggable ranking rules, ties, and multiple views.
Build a Query Engine where filters, sorts and projections are all functions.
Build a Report Builder where every column is a function of the record.
Build a Pipeline Framework that composes stages and reports on each.
Build a Decorator Toolkit of five reusable decorators and apply them together.
Build a CSV Analyser using every tool in the topic.
Build a Text Ranking Engine that scores and ranks documents against a query.
Build a Validation Framework where rules are functions and results are reported per field.
Build a Caching Layer with statistics, using a decorator you write yourself.
Build a Sorting Toolkit demonstrating every key technique in one place.
Explain what a higher-order function is, and show the four ways they appear in Python.
Compare map/filter with comprehensions and generator expressions. Give a rule.
Explain key= completely: what it does, how it performs, and every pattern worth knowing.
Explain decorators completely: what they are, how the syntax works, and what to watch for.
Capstone. Build a Higher Order Toolkit Report using every tool in the topic: map, filter, reduce, sorted(key=), max/min with key, any, all, zip, enumerate, itemgetter, partial, lru_cache and a decorator of your own.
Still stuck on something?
Book a free 1-on-1 session and we'll work through it together.
Book a Free Session