Python · Functional Python

Higher Order Functions — Practice Questions

50 questions. Try each one yourself before checking the answer.

Q1lambdaEasy

Write a lambda that doubles a number, assign it to double, and call it with 5.

Q2map()Easy

Given numbers = [1, 2, 3, 4], use map() with a lambda to double every number, converting the result to a list.

Q3map()Easy

Given numbers = [1, 2, 3, 4], use map() with a regular (non-lambda) function square(n) to square every number.

Q4filter()Easy

Given numbers = [1, 2, 3, 4, 5, 6, 7, 8], use filter() with a lambda to keep only the even numbers.

Q5filter()Easy

Given words = ["apple", "", "banana", "", "cherry"], use filter() with None as the function to drop all "falsy" (empty) values.

Q6any()Easy

Given scores = [45, 60, 30, 85], print whether ANY score is below 40 using any().

Q7all()Easy

Given scores = [45, 60, 30, 85], print whether ALL scores are at least 40 using all().

Q8zip()Easy

Given names = ["Alice", "Bob"] and ages = [30, 25], use zip() to pair them and print the resulting list of tuples.

Q9enumerate()Easy

Given fruits = ["apple", "banana", "cherry"], print each fruit with its index using enumerate().

Q10sorted() keyEasy

Given words = ["banana", "kiwi", "apple", "fig"], sort them by length using sorted() with a lambda key.

Q11map() with Multiple IterablesMedium

Given list1 = [1, 2, 3] and list2 = [10, 20, 30], use map() with a lambda taking two arguments to add them pairwise.

Q12filter()Medium

Given words = ["cat", "elephant", "dog", "hippopotamus"], use filter() to keep only words longer than 4 characters.

Q13sorted() Multi-FieldMedium

Given people = [("Alice", 30), ("Bob", 25), ("Carol", 30)], sort by AGE, then by NAME (as a tiebreaker), using a tuple key.

Q14reduce()Medium

Import reduce from functools and use it to compute the sum of [1, 2, 3, 4, 5] with a lambda.

Q15reduce() with Initial ValueMedium

Use reduce() with an explicit starting value of 100 to add up [1, 2, 3], showing the initial value gets included in the accumulation.

Q16any() with GeneratorMedium

Given usernames = ["alice", "bob123", "carol"], use any() with a generator expression to check if any username contains a digit (via .isdigit() on each character).

Q17all() with GeneratorMedium

Given passwords = ["abc12345", "xyz98765", "short"], use all() to check whether EVERY password is at least 8 characters long.

Q18Chaining map() and filter()Medium

Given numbers = range(1, 11), chain filter() (keep evens) and map() (square them) together.

Q19map() Type ConversionMedium

Given raw_scores = ["88", "92", "75"], use map() with int DIRECTLY (no lambda needed) to convert every value.

Q20enumerate() with startMedium

Given tasks = ["Write report", "Review PR", "Deploy"], print each task numbered starting from 1, using enumerate(tasks, start=1).

Q21Real-WorldMedium

Given celsius_temps = [0, 20, 37, 100], use map() to convert every value to Fahrenheit.

Q22Real-WorldMedium

Given emails = ["alice@test.com", "not-an-email", "bob@test.com"], use filter() to keep only the ones containing "@".

Q23Real-WorldMedium

Use reduce() to compute the factorial of 6, folding a range of numbers together with multiplication.

Q24Real-WorldMedium

Use reduce() to find the MAXIMUM value in [4, 9, 2, 7, 15, 3], without using the built-in max().

Q25Real-WorldMedium

Given students = [{"name": "Alice", "score": 88}, {"name": "Bob", "score": 35}], use any() to check if ANY student failed (score < 40).

Q26Real-WorldMedium

Using the same students list from Q25, use all() to check whether EVERY student passed.

Q27Real-WorldMedium

Given employees = [{"name": "Alice", "salary": 60000}, {"name": "Bob", "salary": 75000}], sort by SALARY descending using sorted() with reverse=True.

Q28Real-WorldMedium

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.

Q29Real-WorldMedium

Given runners = ["Alice", "Bob", "Carol"] (already in finishing order), use enumerate() to print a numbered "podium" list starting from 1st place.

Q30FormattingMedium

Given prices = [10.5, 45.0, 5.75], use map() to format each as currency, then join the results into one printable line.

Q31Functional PipelineHard

Given numbers = range(1, 21), chain filter() (evens), map() (square), and reduce() (sum) into a single functional pipeline computing the total.

Q32reduce()Hard

Use reduce() to build a running MAXIMUM history — not just the final max, but a LIST showing the max-so-far after each step.

Q33sorted() Multi-KeyHard

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.

Q34any() / all()Hard

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.

Q35reduce()Hard

Use reduce() to concatenate a list of words into one sentence, separated by spaces, WITHOUT using .join().

Q36map() Returning TuplesHard

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(*...).

Q37Comprehension vs map/filterHard

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.

Q38reduce() vs LoopHard

Compute the sum of [1, 2, 3, 4, 5] THREE ways — a for loop, sum(), and reduce() — and confirm all three agree.

Q39Sort StabilityHard

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).

Q40operator ModuleHard

Given people = [("Alice", 30), ("Bob", 25)], sort using operator.itemgetter(1) instead of a lambda, and explain why this can be preferred.

Q41Mini-ProjectMini-Project

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.

Q42Mini-ProjectMini-Project

Build an "Employee Ranking System". Given a list of employee dicts, sort by department, then by salary descending, and print a formatted ranked list.

Q43Mini-ProjectMini-Project

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.

Q44Mini-ProjectMini-Project

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.

Q45Mini-ProjectMini-Project

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.

Q46InterviewInterview

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.

Q47InterviewInterview

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.

Q48InterviewInterview

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.

Q49InterviewInterview

Explain when operator.itemgetter/operator.attrgetter are preferable to a lambda as a sorted() key, demonstrating attrgetter on a list of simple objects.

Q50CapstoneInterview

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