0
0
Pythonprogramming~20 mins

map() function in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of map() with lambda and list
What is the output of this Python code?
Python
result = list(map(lambda x: x**2, [1, 2, 3, 4]))
print(result)
A[1, 4, 9, 16]
B[1, 8, 27, 64]
C[2, 4, 6, 8]
D[1, 2, 3, 4]
Attempts:
2 left
๐Ÿ’ก Hint
Think about what the lambda function does to each element.
โ“ Predict Output
intermediate
2:00remaining
map() with multiple iterables
What is the output of this code snippet?
Python
def add(x, y):
    return x + y

result = list(map(add, [1, 2, 3], [4, 5, 6]))
print(result)
A[5, 7, 9]
B[1, 2, 3, 4, 5, 6]
C[4, 5, 6]
D[1, 6, 9]
Attempts:
2 left
๐Ÿ’ก Hint
map applies the function to pairs of elements from both lists.
โ“ Predict Output
advanced
2:00remaining
map() with filter and string conversion
What is the output of this code?
Python
numbers = [10, 15, 20, 25]
result = list(map(str, filter(lambda x: x % 20 == 0, numbers)))
print(result)
A['10', '15', '20', '25']
B['10', '20']
C['20']
D['20', '25']
Attempts:
2 left
๐Ÿ’ก Hint
filter keeps only numbers divisible by 20, then map converts them to strings.
โ“ Predict Output
advanced
2:00remaining
map() with None as function argument
What is the output of this code?
Python
result = list(map(None, [1, 2, 3]))
print(result)
A[]
B[1, 2, 3]
C[None, None, None]
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
In Python 3, map() requires a callable function, None is not callable.
๐Ÿง  Conceptual
expert
3:00remaining
Behavior of map() with infinite iterator
Consider this code: import itertools result = map(lambda x: x*2, itertools.count(1)) output = [] for i, val in enumerate(result): if i == 5: break output.append(val) print(output) What is the output printed?
A[0, 2, 4, 6, 8]
B[2, 4, 6, 8, 10]
C[1, 2, 3, 4, 5]
DInfinite loop, no output
Attempts:
2 left
๐Ÿ’ก Hint
itertools.count(1) generates numbers starting at 1 endlessly; map doubles them; loop stops after 5 items.