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
Using the map() function in Python
๐ Scenario: You work at a fruit store and have a list of fruit prices in dollars. You want to quickly calculate the prices in cents to help with a new pricing system.
๐ฏ Goal: Learn how to use the map() function to apply a calculation to each item in a list.
๐ What You'll Learn
Create a list of fruit prices in dollars
Create a function to convert dollars to cents
Use the map() function with the conversion function and the list
Print the resulting list of prices in cents
๐ก Why This Matters
๐ Real World
Stores and shops often need to convert prices between units like dollars and cents for calculations and display.
๐ผ Career
Understanding map() helps automate repetitive tasks on lists, which is common in data processing and software development.
Progress0 / 4 steps
1
Create the list of fruit prices in dollars
Create a list called prices_dollars with these exact values: 0.5, 1.2, 0.75, 2.0, 1.5
Python
Hint
Use square brackets [] to create a list and separate values with commas.
2
Create a function to convert dollars to cents
Define a function called dollars_to_cents that takes one parameter price and returns price * 100
Python
Hint
Use def to define the function and multiply the input by 100.
3
Use map() to convert all prices to cents
Create a variable called prices_cents that uses map() with the function dollars_to_cents and the list prices_dollars. Convert the result to a list.
Python
Hint
Use list(map(function, list)) to apply the function to each item and get a list back.
4
Print the list of prices in cents
Write a print() statement to display the variable prices_cents
Python
Hint
Use print(prices_cents) to show the final list.
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
Step 1: Understand the purpose of map()
The map() function takes a function and an iterable, then applies the function to each item.
Step 2: Identify the return type
It returns a map object, which can be converted to a list to see the results.
Final Answer:
Applies a given function to each item of an iterable and returns a map object -> Option D
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
Step 1: Check the order of arguments in map()
The first argument must be a function, the second an iterable.
Step 2: Verify the function syntax
Using a lambda function like lambda x: x * 2 is correct to double each item.
Final Answer:
map(lambda x: x * 2, [1, 2, 3]) -> Option A
Quick Check:
Function first, iterable second [OK]
Hint: Function goes first, iterable second in map() [OK]
Mapping squares over [1, 2, 3, 4] gives [1, 4, 9, 16]. Converting map object to list shows these values.
Final Answer:
[1, 4, 9, 16] -> Option A
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
Step 1: Check map() arguments
The map function requires two arguments: a function and an iterable. Here, the iterable is missing.
Step 2: Identify the error cause
Without the iterable, map() cannot apply the function, causing a TypeError.
Final Answer:
Missing iterable argument in map() -> Option B
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
Step 1: Understand map with multiple iterables
map can take multiple iterables and pass corresponding items to the function.
Step 2: Use lambda with two parameters
The lambda takes name and age, then formats the string using f-string for clarity.
Step 3: Convert map object to list
Wrapping with list() shows the final list of formatted strings.
Final Answer:
list(map(lambda n, a: f'{n} is {a}', names, ages)) -> Option C
Quick Check:
map with multiple iterables and f-string [OK]
Hint: Use lambda with multiple args and list() to see results [OK]