Explore programming tutorials, exercises, quizzes, and solutions!
Python Map, Filter, and Reduce Exercises
1/20
In Python, the functions map(), filter(), and reduce() (from the functools module) are powerful tools for functional-style programming. These functions allow you to apply logic to sequences like lists without explicitly writing loops, making your code more expressive and concise.
map() applies a function to each item in an iterable and returns a new iterable with the results.
filter() selects items from an iterable based on whether they satisfy a condition.
reduce() applies a function cumulatively to the items of a sequence, reducing the iterable to a single value.
Consider the following code:
from functools import reduce
nums = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y, nums)
print(result)
What does this code demonstrate?
The code uses reduce() to perform a cumulative operation — in this case, summing all elements of the list [1, 2, 3, 4]. Here's how it works:
reduce(lambda x, y: x + y, nums) starts by applying the lambda to the first two elements: 1 + 2 = 3.
Then it adds the result to the next element: 3 + 3 = 6.
Finally: 6 + 4 = 10.
So result becomes 10, which is printed.
This example highlights the use of reduce() for aggregating data, whereas map() and filter() would return new iterables rather than a single value.
These functional tools make Python concise and expressive when handling data transformation pipelines.
Which of the following will return only odd numbers from a list?
filter() is used when we want to keep only the elements that satisfy a condition. Here, the lambda function checks for odd numbers.
This will include only those elements in the list for which x % 2 != 0 evaluates to True.
Which built-in module provides the reduce() function in Python 3?
In Python 3, reduce() was moved from a built-in to the functools module. You must import it using from functools import reduce.
This decision was made to make functional programming tools more explicit and modular.
What does the following code return?
from functools import reduce
nums = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y, nums)
print(result)
reduce() applies a binary function cumulatively to the elements in the list. It starts with 1 and 2, gets 3, then adds 3 to get 6, and finally adds 4 to reach 10.
The final result is the sum of all numbers in the list: 1 + 2 + 3 + 4 = 10.
Which of the following operations is invalid for map()?
The map() function requires at least two arguments: a function and one or more iterables. In Option 4, the iterable is missing, so this will raise a TypeError.
The other options are valid: Option 1 doubles numbers, Option 2 converts to strings, and Option 3 adds elements from two lists.
What will be the output of the following code?
nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x + 10, nums))
print(result)
The map() function applies the lambda function lambda x: x + 10 to each element in the list [1, 2, 3, 4, 5]. This adds 10 to each number.
The resulting list will be [11, 12, 13, 14, 15].
What does the following code print?
nums = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, nums))
print(result)
The filter() function filters out the numbers that don't meet the condition given in the lambda function. Here, the condition is x % 2 == 0, which selects only the even numbers from the list.
The resulting list will be [2, 4].
What is the output of the following code?
nums = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y ** 2, nums)
print(result)
reduce() applies the lambda function lambda x, y: x + y ** 2 cumulatively on the list. It squares each element and adds them together.
The result will be 1 + 4 + 9 + 16 = 30.
What is the output of the following code?
words = ['apple', 'banana', 'cherry']
result = list(map(lambda word: word.upper(), words))
print(result)
The map() function applies the lambda function lambda word: word.upper() to each word in the list, converting each word to uppercase.
The resulting list will be ['APPLE', 'BANANA', 'CHERRY'].
Which function can be used to find the product of all elements in a list?
The reduce() function is used to apply a binary function cumulatively to the items in a list. Here, the lambda function multiplies each element.
This will calculate the product of all elements in the list, e.g., for nums = [1, 2, 3, 4], the result will be 1 * 2 * 3 * 4 = 24.
What is the output of the following code?
nums = [2, 3, 4, 5]
result = reduce(lambda x, y: x * y, map(lambda z: z ** 2, nums))
print(result)
In this code, the map() function first squares each element in the list, resulting in [4, 9, 16, 25]. Then, reduce() multiplies these squared values cumulatively.
The final result will be 4 * 9 * 16 * 25 = 120.
What will the following code output?
nums = [4, 5, 6, 7]
result = list(filter(lambda x: x % 2 == 0, map(lambda y: y + 1, nums)))
print(result)
The map() function first adds 1 to each number in the list, resulting in [5, 6, 7, 8]. The filter() function then filters the even numbers, returning [6, 8].
What is the output of the following code?
nums = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y * 3, nums)
print(result)
The reduce() function applies the lambda function lambda x, y: x + y * 3 cumulatively to the list. The calculation is done as follows: 1 + (2 * 3) + (3 * 3) + (4 * 3) + (5 * 3) = 70.
What will be the output of the following code?
words = ['apple', 'banana', 'cherry', 'date']
result = list(map(lambda word: word[::-1], words))
print(result)
The map() function applies the lambda function lambda word: word[::-1] to each word, which reverses each string in the list.
The resulting list will be ['elppa', 'ananab', 'yrrehc', 'etad'].
What is the output of the following code?
nums = [10, 20, 30, 40, 50]
result = reduce(lambda x, y: x + y // 2, nums)
print(result)
The reduce() function applies the lambda function lambda x, y: x + y // 2 cumulatively to the list. The integer division // is performed on each element. The final result is 10 + 10 + 15 + 20 + 25 = 85.
What will be the output of the following code?
nums = [1, 2, 3, 4, 5, 6]
result = reduce(lambda x, y: x + y if x % 2 == 0 else x - y, nums)
print(result)
The reduce() function applies the lambda function lambda x, y: x + y if x % 2 == 0 else x - y cumulatively to the list. It adds y when x is even, and subtracts y when x is odd.
The calculations will be: 1 - 2 = -1, -1 + 3 = 2, 2 - 4 = -2, -2 + 5 = 3, 3 - 6 = -3. The final result will be -1.
What is the output of the following code?
nums = [4, 3, 2, 1]
result = reduce(lambda x, y: x - y ** 2, nums)
print(result)
The reduce() function applies the lambda function lambda x, y: x - y ** 2 cumulatively to the list, subtracting the square of each number from the result.
words = ['cat', 'dog', 'bird', 'fish']
result = reduce(lambda x, y: x + len(y), words, 0)
print(result)
The reduce() function applies the lambda function lambda x, y: x + len(y) cumulatively, adding the length of each word in the list. The initial value is 0.
The lengths of the words are: cat = 3, dog = 3, bird = 4, fish = 4. The total length is 3 + 3 + 4 + 4 = 14.
What is the output of the following code?
nums = [2, 3, 4, 5]
result = list(map(lambda x: x * 2 if x % 2 == 0 else x * 3, nums))
print(result)
The map() function applies the lambda function lambda x: x * 2 if x % 2 == 0 else x * 3 to each element. If x is even, it is multiplied by 2; if x is odd, it is multiplied by 3.
The resulting list will be: [4, 9, 8, 15].
What is the output of the following code?
nums = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, map(lambda x: x + 1, nums)))
print(result)
The map() function adds 1 to each number in the list, resulting in [2, 3, 4, 5, 6]. Then, the filter() function selects only the even numbers, resulting in [2, 4].
Practicing Python Map, Filter, and Reduce? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Map, Filter, and Reduce
Welcome to the Python Map, Filter, and Reduce exercises — a practical and comprehensive set of challenges designed to help you master three powerful higher-order functions used extensively in functional programming. These functions allow you to process and transform data efficiently and concisely, making your Python code more expressive and readable.
Map, Filter, and Reduce are essential tools for applying functions to sequences like lists or tuples. map() applies a function to every item in an iterable, filter() selects items based on a condition, and reduce() (from the functools module) reduces an iterable to a single cumulative value. Through these exercises, you’ll learn how to use these functions individually and together to solve complex data processing problems.
Starting with basic examples, you’ll practice using map() to transform lists, filter() to extract subsets of data, and reduce() to aggregate results. You’ll also explore how to combine these functions with lambda expressions to write concise and elegant code. As you progress, you’ll tackle more advanced scenarios such as chaining these functions, working with custom functions, and handling edge cases.
Mastering Map, Filter, and Reduce is especially useful for data scientists, software developers, and anyone working with large datasets or streams of information. These functions help you write code that is both efficient and easy to understand, replacing verbose loops with clean functional constructs.
In addition to coding practice, this section emphasizes best practices like choosing the right function for your task, balancing readability with conciseness, and understanding when traditional loops might be more appropriate. These exercises will also prepare you for coding interviews where these concepts frequently appear.
To deepen your learning, we recommend exploring related Python topics such as list comprehensions, generator expressions, and the functools module. These combined skills will expand your ability to write elegant, high-performance Python code.
Start practicing the Python Map, Filter, and Reduce exercises today to elevate your functional programming skills in Python. With regular practice, you’ll gain confidence in processing and transforming data effectively using these powerful tools.