Chapter 18 Intermediate 58 Questions

Practice Questions — Lambda, Map, Filter, and Reduce

← Back to Notes
9 Easy
12 Medium
8 Hard

Topic-Specific Questions

Question 1
Easy
What is the output of the following code?
square = lambda x: x ** 2
print(square(4))
print(square(7))
Lambda works like a regular function. x ** 2 is returned automatically.
16
49
Question 2
Easy
What is the output?
add = lambda a, b: a + b
print(add(3, 5))
print(add("Hello ", "World"))
The + operator works for both numbers and strings.
8
Hello World
Question 3
Easy
What is the output?
numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, numbers))
print(result)
map() applies the function to every element.
[2, 4, 6, 8, 10]
Question 4
Easy
What is the output?
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
filter() keeps elements where the function returns True.
[2, 4, 6]
Question 5
Easy
What is the output?
check = lambda x: "even" if x % 2 == 0 else "odd"
print(check(4))
print(check(7))
Lambda can use conditional expressions (ternary).
even
odd
Question 6
Easy
What is the output?
words = ["banana", "pie", "apple", "fig"]
result = sorted(words, key=lambda w: len(w))
print(result)
key=lambda w: len(w) tells sorted() to compare by length.
['pie', 'fig', 'apple', 'banana']
Question 7
Medium
What is the output?
from functools import reduce
result = reduce(lambda a, b: a + b, [1, 2, 3, 4, 5])
print(result)
reduce applies cumulatively: ((((1+2)+3)+4)+5).
15
Question 8
Medium
What is the output?
from functools import reduce
result = reduce(lambda a, b: a * b, [1, 2, 3, 4, 5])
print(result)
This computes the product of all elements.
120
Question 9
Medium
What is the output?
names = ["aarav", "priya", "rohan"]
result = list(map(str.upper, names))
print(result)
str.upper is an existing method. No lambda needed.
['AARAV', 'PRIYA', 'ROHAN']
Question 10
Medium
What is the output?
data = [("Aarav", 85), ("Priya", 92), ("Rohan", 78)]
result = sorted(data, key=lambda x: x[1], reverse=True)
print(result)
key=lambda x: x[1] sorts by the second element. reverse=True for descending.
[('Priya', 92), ('Aarav', 85), ('Rohan', 78)]
Question 11
Medium
What is the output?
mixed = [0, 1, "", "hello", None, 42, False, "world"]
result = list(filter(None, mixed))
print(result)
filter(None, ...) removes all falsy values.
[1, 'hello', 42, 'world']
Question 12
Medium
What is the output?
numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x ** 2, filter(lambda x: x > 2, numbers)))
print(result)
filter first (keep x > 2), then map (square them).
[9, 16, 25]
Question 13
Hard
What is the output?
from functools import reduce
result = reduce(lambda a, b: a if a > b else b, [3, 7, 2, 9, 4])
print(result)
This reduce keeps the larger of each pair, finding the maximum.
9
Question 14
Hard
What is the output?
from functools import reduce
result = reduce(lambda a, b: a + b, [1, 2, 3], 100)
print(result)
reduce with an initial value starts accumulation from that value.
106
Question 15
Hard
What is the output?
students = [
    {"name": "Aarav", "marks": 85},
    {"name": "Priya", "marks": 92},
    {"name": "Rohan", "marks": 78}
]
topper = max(students, key=lambda s: s["marks"])
print(topper["name"])
max() with key=lambda finds the element with the highest value for the given key.
Priya
Question 16
Hard
What is the output?
pairs = [("Aarav", 85), ("Priya", 92), ("Rohan", 85), ("Meera", 92)]
result = sorted(pairs, key=lambda x: (-x[1], x[0]))
print(result)
Negative marks for descending order, then name for ascending (alphabetical).
[('Meera', 92), ('Priya', 92), ('Aarav', 85), ('Rohan', 85)]
Question 17
Hard
What is the output?
funcs = [lambda x: x + 1, lambda x: x * 2, lambda x: x ** 2]
result = [f(3) for f in funcs]
print(result)
Each lambda is applied to the argument 3.
[4, 6, 9]

Mixed & Application Questions

Question 1
Easy
What is the output?
f = lambda: 42
print(f())
print(type(f))
A lambda with no parameters just returns the expression.
42
<class 'function'>
Question 2
Easy
What is the output?
nums = ["3", "1", "4", "1", "5"]
result = list(map(int, nums))
print(result)
print(sum(result))
map(int, ...) converts each string to an integer.
[3, 1, 4, 1, 5]
14
Question 3
Easy
What is the output?
words = ["hello", "world", "python"]
lengths = list(map(len, words))
print(lengths)
map(len, ...) applies len() to each element.
[5, 5, 6]
Question 4
Medium
What is the output?
numbers = [1, 2, 3, 4, 5]
odd_squares = list(map(lambda x: x**2, filter(lambda x: x % 2 != 0, numbers)))
print(odd_squares)
filter keeps odd numbers, then map squares them.
[1, 9, 25]
Question 5
Medium
What is the output?
a = [1, 2, 3]
b = [10, 20, 30]
result = list(map(lambda x, y: x + y, a, b))
print(result)
map with two iterables passes one element from each to the lambda.
[11, 22, 33]
Question 6
Medium
What is the output?
words = ["Python", "is", "awesome", "!"]
longest = max(words, key=lambda w: len(w))
shortest = min(words, key=lambda w: len(w))
print(longest)
print(shortest)
max/min with key=len finds the longest/shortest string.
awesome
!
Question 7
Medium
What is the output?
result = (lambda x, y: x ** y)(2, 10)
print(result)
This is an immediately invoked lambda expression (IIFE).
1024
Question 8
Medium
What is the difference between map() and filter()?
One transforms, the other selects.
map(func, iterable) transforms every element by applying func and returns the results. filter(func, iterable) selects elements where func returns True. map changes values, filter changes which values are included.
Question 9
Medium
When should you use a lambda instead of a def function?
Think about function complexity and reusability.
Use lambda for short, simple, throwaway operations used once (like sort keys, quick transforms in map/filter). Use def when the function is complex, needs multiple lines, will be reused, or needs a docstring.
Question 10
Hard
What is the output?
from functools import reduce
nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flat = reduce(lambda a, b: a + b, nested)
print(flat)
print(len(flat))
The + operator on lists concatenates them. reduce applies it cumulatively.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
9
Question 11
Hard
What is the output?
marks = {"Aarav": 85, "Priya": 45, "Rohan": 92, "Meera": 38}
passed = dict(filter(lambda item: item[1] >= 50, marks.items()))
print(passed)
filter on dict.items() gives key-value tuples. Convert back to dict.
{'Aarav': 85, 'Rohan': 92}
Question 12
Hard
What is the output?
from functools import reduce
words = ["Hello", " ", "World", "!"]
result = reduce(lambda a, b: a + b, words)
print(result)
print(type(result))
reduce with string concatenation joins all strings into one.
Hello World!
<class 'str'>

Multiple Choice Questions

MCQ 1
What is the correct syntax for a lambda function?
  • A. lambda(x): x + 1
  • B. lambda x: x + 1
  • C. def lambda(x): x + 1
  • D. lambda x -> x + 1
Answer: B
B is correct. Lambda syntax is lambda params: expression. No parentheses around parameters, no arrow notation, and no def keyword.
MCQ 2
What does map() return in Python 3?
  • A. A list
  • B. A tuple
  • C. A map object (iterator)
  • D. A dictionary
Answer: C
C is correct. In Python 3, map() returns a lazy map object (iterator), not a list. Use list(map(...)) to get a list. This is memory-efficient for large datasets.
MCQ 3
What does filter() do?
  • A. Transforms every element
  • B. Keeps elements where the function returns True
  • C. Removes duplicate elements
  • D. Sorts elements by condition
Answer: B
B is correct. filter(func, iterable) tests each element with func and keeps only those where func returns True. It does not transform elements (that is map) or remove duplicates (that is set).
MCQ 4
Where is reduce() located in Python 3?
  • A. It is a built-in function
  • B. In the math module
  • C. In the functools module
  • D. In the itertools module
Answer: C
C is correct. reduce() was moved to functools in Python 3. You must write from functools import reduce before using it.
MCQ 5
Can a lambda function contain an if-else statement?
  • A. No, lambdas cannot have any conditions
  • B. Yes, using a conditional expression (ternary): a if condition else b
  • C. Yes, using a regular if-else block
  • D. Only in Python 3.10+
Answer: B
B is correct. Lambda can use conditional expressions (ternary): lambda x: 'yes' if x > 0 else 'no'. It cannot use if-else statements (blocks with colons and indentation).
MCQ 6
What is the output of list(map(lambda x: x ** 2, [1, 2, 3]))?
  • A. [1, 2, 3]
  • B. [2, 4, 6]
  • C. [1, 4, 9]
  • D. (1, 4, 9)
Answer: C
C is correct. map applies x ** 2 to each element: 1**2=1, 2**2=4, 3**2=9. Result: [1, 4, 9].
MCQ 7
What is the output of list(filter(lambda x: x > 3, [1, 2, 3, 4, 5]))?
  • A. [1, 2, 3]
  • B. [4, 5]
  • C. [3, 4, 5]
  • D. [True, True]
Answer: B
B is correct. filter keeps elements where x > 3 is True. Only 4 and 5 satisfy this condition. Result: [4, 5].
MCQ 8
What does filter(None, [0, 1, '', 'hello', None, 42]) return?
  • A. [0, 1, '', 'hello', None, 42]
  • B. [1, 'hello', 42]
  • C. [None]
  • D. []
Answer: B
B is correct. When func is None, filter removes all falsy values. 0, '', and None are falsy. 1, 'hello', and 42 are truthy and are kept.
MCQ 9
What does sorted(['pie', 'apple', 'fig'], key=lambda x: len(x)) return?
  • A. ['apple', 'fig', 'pie']
  • B. ['pie', 'fig', 'apple']
  • C. ['fig', 'pie', 'apple']
  • D. [3, 3, 5]
Answer: B
B is correct. Lengths: pie=3, fig=3, apple=5. Sorted by length: pie and fig (both 3) come first (original order preserved), then apple (5). Result: ['pie', 'fig', 'apple'].
MCQ 10
Which is NOT a valid lambda expression?
  • A. lambda x: x + 1
  • B. lambda: 42
  • C. lambda x, y: x * y
  • D. lambda x: print(x); return x
Answer: D
D is correct. Lambda cannot contain statements like return or semicolons separating multiple statements. Lambda can only have a single expression. Options A, B, and C are all valid.
MCQ 11
What is the output of reduce(lambda a, b: a * b, [1, 2, 3, 4])?
  • A. 10
  • B. 24
  • C. [2, 6, 24]
  • D. Error
Answer: B
B is correct. reduce computes the product cumulatively: 1*2=2, 2*3=6, 6*4=24. The final result is 24 (factorial of 4).
MCQ 12
What is the difference between lambda and def?
  • A. Lambda is faster than def
  • B. Lambda can only contain a single expression; def can have multiple statements
  • C. Lambda can have a docstring; def cannot
  • D. There is no difference
Answer: B
B is correct. Lambda is limited to a single expression (no statements, no multi-line body). def can have multiple statements, docstrings, and complex logic. Lambda is not faster; both create function objects.
MCQ 13
What does map() do when given two iterables of different lengths?
  • A. Raises a ValueError
  • B. Pads the shorter one with None
  • C. Stops at the shortest iterable
  • D. Repeats the shorter iterable
Answer: C
C is correct. map() stops when the shortest iterable is exhausted. For example, list(map(lambda x, y: x+y, [1, 2, 3], [10, 20])) gives [11, 22] (stops after 2 elements).
MCQ 14
What is reduce(lambda a, b: a + b, [5], 10)?
  • A. 5
  • B. 15
  • C. 10
  • D. Error
Answer: B
B is correct. The initial value is 10. reduce computes: 10 + 5 = 15. The initial value is combined with the first (and only) element.
MCQ 15
Which is more readable for squaring a list of numbers?
  • A. list(map(lambda x: x**2, numbers))
  • B. [x**2 for x in numbers]
  • C. Both are equally readable
  • D. Neither is readable
Answer: B
B is correct. For simple transformations, list comprehensions are generally more readable and Pythonic than map with lambda. The Python community and PEP 8 favor comprehensions for simple cases.
MCQ 16
What does lambda x, y: x + y do?
  • A. Creates a function that prints x + y
  • B. Creates a function that returns x + y
  • C. Assigns x + y to a variable
  • D. Creates two variables x and y
Answer: B
B is correct. Lambda creates an anonymous function that takes two parameters (x and y) and returns their sum. The return is implicit.
MCQ 17
What does sorted(data, key=lambda x: (-x[1], x[0])) do?
  • A. Sorts by first element descending, then second ascending
  • B. Sorts by second element descending, then first element ascending
  • C. Sorts by second element ascending, then first descending
  • D. Raises an error because of the negative sign
Answer: B
B is correct. Negating the second element (-x[1]) reverses its sort order (descending). The first element (x[0]) is not negated, so it sorts ascending. This is a common multi-criteria sort pattern.
MCQ 18
What is the output of list(filter(lambda x: x, [0, 1, 2, 0, 3]))?
  • A. [0, 1, 2, 0, 3]
  • B. [1, 2, 3]
  • C. [0, 0]
  • D. []
Answer: B
B is correct. The lambda returns x itself. filter keeps elements that are truthy. 0 is falsy, so it is removed. 1, 2, and 3 are truthy, so they are kept.

Coding Challenges

Challenge 1: Square All Numbers Using map()

Easy
Given the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], use map() with a lambda to create a new list of their squares.
Sample Input
(No input required)
Sample Output
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Use map() and lambda. Do not use list comprehension.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares = list(map(lambda x: x ** 2, numbers))
print(squares)

Challenge 2: Filter Passing Students

Easy
Given marks = {'Aarav': 85, 'Priya': 45, 'Rohan': 92, 'Meera': 38, 'Vikram': 67}, use filter() to create a new dictionary containing only students who scored 50 or above.
Sample Input
(No input required)
Sample Output
{'Aarav': 85, 'Rohan': 92, 'Vikram': 67}
Use filter() with lambda on dict.items().
marks = {'Aarav': 85, 'Priya': 45, 'Rohan': 92, 'Meera': 38, 'Vikram': 67}
passed = dict(filter(lambda item: item[1] >= 50, marks.items()))
print(passed)

Challenge 3: Sort Students by Marks

Easy
Given a list of tuples: students = [('Aarav', 85), ('Priya', 92), ('Rohan', 78), ('Meera', 95)], sort them by marks in descending order using sorted() with key=lambda.
Sample Input
(No input required)
Sample Output
[('Meera', 95), ('Priya', 92), ('Aarav', 85), ('Rohan', 78)]
Use sorted() with key=lambda and reverse=True.
students = [('Aarav', 85), ('Priya', 92), ('Rohan', 78), ('Meera', 95)]
result = sorted(students, key=lambda s: s[1], reverse=True)
print(result)

Challenge 4: Celsius to Fahrenheit with map()

Medium
Given temperatures in Celsius: [0, 20, 37, 100], use map() with a lambda to convert each to Fahrenheit (F = C * 9/5 + 32). Print the result rounded to 1 decimal place.
Sample Input
(No input required)
Sample Output
[32.0, 68.0, 98.6, 212.0]
Use map() with lambda. Use round() inside the lambda.
celsius = [0, 20, 37, 100]
fahrenheit = list(map(lambda c: round(c * 9/5 + 32, 1), celsius))
print(fahrenheit)

Challenge 5: Product of a List Using reduce()

Medium
Use reduce() to compute the product of all numbers in [2, 3, 5, 7, 11] (multiply all elements together).
Sample Input
(No input required)
Sample Output
2310
Must use reduce() from functools.
from functools import reduce
numbers = [2, 3, 5, 7, 11]
product = reduce(lambda a, b: a * b, numbers)
print(product)

Challenge 6: Extract and Sort Names by Length

Medium
Given records = [{'name': 'Aarav', 'age': 16}, {'name': 'Priya', 'age': 15}, {'name': 'Vikramaditya', 'age': 17}, {'name': 'Meera', 'age': 14}], use map() to extract just the names, then sort them by length (shortest first).
Sample Input
(No input required)
Sample Output
['Aarav', 'Priya', 'Meera', 'Vikramaditya']
Use map() to extract names and sorted() with key=lambda for sorting.
records = [
    {'name': 'Aarav', 'age': 16},
    {'name': 'Priya', 'age': 15},
    {'name': 'Vikramaditya', 'age': 17},
    {'name': 'Meera', 'age': 14}
]
names = list(map(lambda r: r['name'], records))
sorted_names = sorted(names, key=lambda n: len(n))
print(sorted_names)

Challenge 7: Chain Filter and Map: High Scorer Names

Medium
Given students = [{'name': 'Aarav', 'marks': 85}, {'name': 'Priya', 'marks': 92}, {'name': 'Rohan', 'marks': 78}, {'name': 'Meera', 'marks': 95}, {'name': 'Vikram', 'marks': 60}], use filter() to keep students scoring above 80, then map() to extract just their names.
Sample Input
(No input required)
Sample Output
['Aarav', 'Priya', 'Meera']
Chain filter() and map() with lambda.
students = [
    {'name': 'Aarav', 'marks': 85},
    {'name': 'Priya', 'marks': 92},
    {'name': 'Rohan', 'marks': 78},
    {'name': 'Meera', 'marks': 95},
    {'name': 'Vikram', 'marks': 60}
]
result = list(map(lambda s: s['name'], filter(lambda s: s['marks'] > 80, students)))
print(result)

Challenge 8: Multi-Criteria Sort

Hard
Given data = [('Aarav', 'A', 85), ('Priya', 'A', 92), ('Rohan', 'B', 85), ('Meera', 'A', 92), ('Vikram', 'B', 78)], sort by: grade ascending, then marks descending, then name ascending.
Sample Input
(No input required)
Sample Output
[('Meera', 'A', 92), ('Priya', 'A', 92), ('Aarav', 'A', 85), ('Rohan', 'B', 85), ('Vikram', 'B', 78)]
Use sorted() with a lambda that returns a tuple key.
data = [('Aarav', 'A', 85), ('Priya', 'A', 92), ('Rohan', 'B', 85), ('Meera', 'A', 92), ('Vikram', 'B', 78)]
result = sorted(data, key=lambda x: (x[1], -x[2], x[0]))
print(result)

Challenge 9: Frequency Count with reduce()

Hard
Use reduce() to count the frequency of each character in the string 'mississippi'. The result should be a dictionary.
Sample Input
(No input required)
Sample Output
{'m': 1, 'i': 4, 's': 4, 'p': 2}
Must use reduce(). The accumulator should be a dictionary.
from functools import reduce

text = 'mississippi'
freq = reduce(
    lambda acc, ch: {**acc, ch: acc.get(ch, 0) + 1},
    text,
    {}
)
print(freq)

Challenge 10: Data Processing Pipeline

Hard
Given raw data: raw = [' Aarav:85 ', ' Priya:92 ', ' Rohan:78 ', ' Meera:95 ', ' Vikram:60 '], use map/filter/lambda to: (1) strip whitespace, (2) split by ':', (3) convert marks to int, (4) filter marks > 80, (5) print names of students who passed.
Sample Input
(No input required)
Sample Output
['Aarav', 'Priya', 'Meera']
Use a combination of map() and filter() with lambda.
raw = ['  Aarav:85  ', '  Priya:92  ', '  Rohan:78  ', '  Meera:95  ', '  Vikram:60  ']

# Step 1-3: Clean and parse
parsed = list(map(lambda s: s.strip().split(':'), raw))
parsed = list(map(lambda p: (p[0], int(p[1])), parsed))

# Step 4-5: Filter and extract names
result = list(map(lambda p: p[0], filter(lambda p: p[1] > 80, parsed)))
print(result)

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn Python with a live mentor?

Explore our Python course