Bird
Raised Fist0
Pythonprogramming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the map() function do in Python?
easy
A. Combines two lists into a dictionary
B. Creates a new list by filtering items from an iterable
C. Sorts the items of a list in ascending order
D. Applies a given function to each item of an iterable and returns a map object

Solution

  1. Step 1: Understand the purpose of map()

    The map() function takes a function and an iterable, then applies the function to each item.
  2. Step 2: Identify the return type

    It returns a map object, which can be converted to a list to see the results.
  3. Final Answer:

    Applies a given function to each item of an iterable and returns a map object -> Option D
  4. Quick Check:

    map() applies function to items [OK]
Hint: Remember: map applies function to each item [OK]
Common Mistakes:
  • Confusing map() with filter()
  • Thinking map() returns a list directly
  • Assuming map() sorts or combines lists
2. Which of the following is the correct syntax to use map() to double each number in the list [1, 2, 3]?
easy
A. map(lambda x: x * 2, [1, 2, 3])
B. map([1, 2, 3], lambda x: x * 2)
C. map(x * 2, [1, 2, 3])
D. map(lambda x: x * 2)

Solution

  1. Step 1: Check the order of arguments in map()

    The first argument must be a function, the second an iterable.
  2. Step 2: Verify the function syntax

    Using a lambda function like lambda x: x * 2 is correct to double each item.
  3. Final Answer:

    map(lambda x: x * 2, [1, 2, 3]) -> Option A
  4. Quick Check:

    Function first, iterable second [OK]
Hint: Function goes first, iterable second in map() [OK]
Common Mistakes:
  • Swapping function and iterable arguments
  • Omitting the iterable argument
  • Passing an expression instead of a function
3. What is the output of the following code?
nums = [1, 2, 3, 4]
squared = map(lambda x: x**2, nums)
print(list(squared))
medium
A. [1, 4, 9, 16]
B. [2, 4, 6, 8]
C. [1, 2, 3, 4]
D. Error: map object cannot be converted to list

Solution

  1. Step 1: Understand the lambda function

    The lambda function squares each number: x**2.
  2. Step 2: Apply map and convert to list

    Mapping squares over [1, 2, 3, 4] gives [1, 4, 9, 16]. Converting map object to list shows these values.
  3. Final Answer:

    [1, 4, 9, 16] -> Option A
  4. Quick Check:

    Squares of 1 to 4 = [1, 4, 9, 16] [OK]
Hint: Convert map to list to see results [OK]
Common Mistakes:
  • Forgetting to convert map object to list
  • Confusing square with double
  • Expecting map to print directly
4. What is wrong with this code snippet?
nums = [1, 2, 3]
result = map(lambda x: x + 1)
print(list(result))
medium
A. Lambda function syntax is incorrect
B. Missing iterable argument in map()
C. Cannot convert map object to list
D. Using print inside map is invalid

Solution

  1. Step 1: Check map() arguments

    The map function requires two arguments: a function and an iterable. Here, the iterable is missing.
  2. Step 2: Identify the error cause

    Without the iterable, map() cannot apply the function, causing a TypeError.
  3. Final Answer:

    Missing iterable argument in map() -> Option B
  4. Quick Check:

    map needs function and iterable [OK]
Hint: Always provide both function and iterable to map() [OK]
Common Mistakes:
  • Forgetting the iterable argument
  • Assuming map works with one argument
  • Misunderstanding lambda function usage
5. You have two lists: names = ['alice', 'bob', 'carol'] and ages = [25, 30, 22]. Using map(), how can you create a list of strings like ['alice is 25', 'bob is 30', 'carol is 22']?
hard
A. list(map(lambda x: x[0] + ' is ' + x[1], names, ages))
B. map(lambda n, a: n + ' is ' + a, names, ages)
C. list(map(lambda n, a: f'{n} is {a}', names, ages))
D. list(map(lambda n: n + ' is ' + ages, names))

Solution

  1. Step 1: Understand map with multiple iterables

    map can take multiple iterables and pass corresponding items to the function.
  2. Step 2: Use lambda with two parameters

    The lambda takes name and age, then formats the string using f-string for clarity.
  3. Step 3: Convert map object to list

    Wrapping with list() shows the final list of formatted strings.
  4. Final Answer:

    list(map(lambda n, a: f'{n} is {a}', names, ages)) -> Option C
  5. Quick Check:

    map with multiple iterables and f-string [OK]
Hint: Use lambda with multiple args and list() to see results [OK]
Common Mistakes:
  • Passing only one iterable when two are needed
  • Concatenating string and int without conversion
  • Not converting map object to list