Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to apply the function str.upper to each element in the list words using map().
Data Analysis Python
words = ['apple', 'banana', 'cherry'] result = list(map([1], words)) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
str.upper() instead of str.upper.Passing a string like 'upper' instead of the function.
✗ Incorrect
The
map() function requires a function object as the first argument. str.upper is the method reference, while str.upper() calls the method immediately, which is incorrect here.2fill in blank
mediumComplete the code to convert all numbers in the list nums to strings using map().
Data Analysis Python
nums = [1, 2, 3, 4] str_nums = list(map([1], nums)) print(str_nums)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
int which does not convert numbers to strings.Using a lambda that adds 1 instead of converting to string.
✗ Incorrect
The
str function converts numbers to strings, which is what we want to apply to each element.3fill in blank
hardFix the error in the code to correctly apply the function abs to each element in the list values using map().
Data Analysis Python
values = [-1, -2, 3] result = list(map([1], values)) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
abs() which calls the function immediately.Trying to use
abs(x) directly without a lambda.✗ Incorrect
The
map() function needs a function object, so abs without parentheses is correct. abs() calls the function immediately and is incorrect here.4fill in blank
hardFill both blanks to create a list of squares of numbers greater than 3 using map() and filter().
Data Analysis Python
numbers = [1, 2, 3, 4, 5] filtered = filter([1], numbers) squares = list(map([2], filtered)) print(squares)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong filter condition like
x < 3.Using map function that doubles instead of squares.
✗ Incorrect
The
filter() uses a function that returns True for numbers greater than 3. The map() applies the square function to each filtered number.5fill in blank
hardFill all three blanks to create a dictionary mapping each word to its length, but only for words longer than 4 characters, using filter() and dictionary comprehension.
Data Analysis Python
words = ['apple', 'bat', 'carrot', 'dog'] lengths = { [1]: [2] for [3] in filter(lambda word: len(word) > 4, words) } print(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names between filter and comprehension.
Using wrong expressions for keys or values.
✗ Incorrect
The dictionary comprehension uses
word as the variable from the filter. The key is the word itself, and the value is its length.