Bird
Raised Fist0
Pythonprogramming~20 mins

Lambda with map() 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
๐ŸŽ–๏ธ
Lambda Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of lambda with map() on a list
What is the output of this Python code?
Python
numbers = [1, 2, 3, 4]
result = list(map(lambda x: x * 3, numbers))
print(result)
A[1, 4, 9, 16]
B[1, 2, 3, 4]
C[3, 6, 9, 12]
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Remember map applies the function to each item in the list.
โ“ Predict Output
intermediate
2:00remaining
Result of map with lambda and filter logic
What is the output of this code snippet?
Python
values = [0, 1, 2, 3, 4]
result = list(map(lambda x: x if x % 2 == 0 else 0, values))
print(result)
ASyntaxError
B[0, 1, 2, 3, 4]
C[0, 1, 0, 3, 0]
D[0, 0, 2, 0, 4]
Attempts:
2 left
๐Ÿ’ก Hint
The lambda replaces odd numbers with zero.
โ“ Predict Output
advanced
2:00remaining
Output of nested lambda with map on strings
What does this code print?
Python
words = ['apple', 'banana', 'cherry']
result = list(map(lambda w: list(map(lambda c: c.upper(), w)), words))
print(result)
A['APPLE', 'BANANA', 'CHERRY']
B[['A', 'P', 'P', 'L', 'E'], ['B', 'A', 'N', 'A', 'N', 'A'], ['C', 'H', 'E', 'R', 'R', 'Y']]
C[['apple'], ['banana'], ['cherry']]
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
The inner map converts each character to uppercase.
โ“ Predict Output
advanced
2:00remaining
Output of map with lambda and multiple iterables
What is the output of this code?
Python
a = [1, 2, 3]
b = [4, 5, 6]
result = list(map(lambda x, y: x + y, a, b))
print(result)
A[5, 7, 9]
B[1, 2, 3, 4, 5, 6]
C[4, 10, 18]
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
map applies the lambda to pairs of elements from both lists.
๐Ÿง  Conceptual
expert
2:00remaining
Error raised by incorrect lambda syntax in map
What error does this code raise?
Python
result = list(map(lambda x: x * 2 if x > 2 for x in range(5)))
ASyntaxError
BTypeError
CNameError
DNo error, outputs [6, 8]
Attempts:
2 left
๐Ÿ’ก Hint
Check the lambda syntax and how map is used.

Practice

(1/5)
1.

What does the map() function do when used with a lambda in Python?

easy
A. Creates a new list without changing items
B. Filters items based on a condition
C. Applies the lambda function to each item in the list
D. Sorts the list items

Solution

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

    map() takes a function and a list, then applies the function to each item.
  2. Step 2: Role of lambda with map()

    The lambda defines a quick function to apply to each item in the list.
  3. Final Answer:

    Applies the lambda function to each item in the list -> Option C
  4. Quick Check:

    map() + lambda = apply function to each item [OK]
Hint: Remember: map applies function to every list item [OK]
Common Mistakes:
  • Thinking map filters items
  • Assuming map sorts the list
  • Believing map returns the original list unchanged
2.

Which of the following is the correct syntax to double each number in the list [1, 2, 3] using map() and lambda?

?
easy
A. map(lambda x: x * 2, [1, 2, 3])
B. list(map(lambda x: x * 2, [1, 2, 3]))
C. list(lambda x: x * 2, [1, 2, 3])
D. map([1, 2, 3], lambda x: x * 2)

Solution

  1. Step 1: Check map() syntax

    map() takes a function first, then the iterable (list).
  2. Step 2: Convert map object to list

    To see results, wrap map() with list().
  3. Final Answer:

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

    Correct syntax = list(map(lambda x: x * 2, list)) [OK]
Hint: Use list() around map() to see results [OK]
Common Mistakes:
  • Forgetting to convert map to list
  • Swapping arguments in map()
  • Using lambda outside map() incorrectly
3.

What is the output of the following code?

nums = [1, 2, 3, 4]
result = list(map(lambda x: x + 1, nums))
print(result)
medium
A. [2, 3, 4, 5]
B. [1, 2, 3, 4]
C. [0, 1, 2, 3]
D. Error

Solution

  1. Step 1: Understand the lambda function

    The lambda adds 1 to each number in the list.
  2. Step 2: Apply lambda to each item in nums

    Each number 1,2,3,4 becomes 2,3,4,5 respectively.
  3. Final Answer:

    [2, 3, 4, 5] -> Option A
  4. Quick Check:

    Each item +1 = [2,3,4,5] [OK]
Hint: Add 1 to each item with lambda x: x + 1 [OK]
Common Mistakes:
  • Forgetting to convert map to list before print
  • Confusing addition with multiplication
  • Expecting original list unchanged
4.

Find the error in this code snippet:

numbers = [1, 2, 3]
result = map(lambda x: x ** 2, numbers)
print(result)
medium
A. map() requires two arguments but only one given
B. Lambda syntax is incorrect
C. List 'numbers' is not defined
D. Missing list() to convert map object before printing

Solution

  1. Step 1: Check map() usage

    map() returns a map object, not a list.
  2. Step 2: Printing map object directly

    Printing map object shows memory address, not values; convert to list first.
  3. Final Answer:

    Missing list() to convert map object before printing -> Option D
  4. Quick Check:

    Use list() to see map results [OK]
Hint: Wrap map() with list() before printing [OK]
Common Mistakes:
  • Trying to print map object directly
  • Thinking lambda syntax is wrong
  • Assuming map needs more arguments
5.

You have a list of strings representing numbers: ['1', '2', '3', '4']. Using map() and lambda, how do you convert this list to integers and then add 10 to each number?

hard
A. list(map(lambda x: int(x) + 10, ['1', '2', '3', '4']))
B. list(map(lambda x: x + 10, ['1', '2', '3', '4']))
C. list(map(int(x) + 10, ['1', '2', '3', '4']))
D. list(map(lambda x: int(x + 10), ['1', '2', '3', '4']))

Solution

  1. Step 1: Convert strings to integers inside lambda

    Use int(x) to convert each string to an integer.
  2. Step 2: Add 10 to each converted integer

    Inside lambda, add 10 to the integer value.
  3. Final Answer:

    list(map(lambda x: int(x) + 10, ['1', '2', '3', '4'])) -> Option A
  4. Quick Check:

    Convert then add 10 = list(map(lambda x: int(x)+10, list)) [OK]
Hint: Convert string to int inside lambda before adding [OK]
Common Mistakes:
  • Adding 10 to string without conversion
  • Incorrect lambda syntax
  • Trying to call int() outside lambda in map