0
0
Data Analysis Pythondata~10 mins

map() for element-wise mapping in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aupper
Blambda x: x.lower()
Cstr.upper()
Dstr.upper
Attempts:
3 left
💡 Hint
Common Mistakes
Using str.upper() instead of str.upper.
Passing a string like 'upper' instead of the function.
2fill in blank
medium

Complete 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'
Astr
Bfloat
Cint
Dlambda x: x + 1
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.
3fill in blank
hard

Fix 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'
Aabs()
Babs
Clambda x: abs(x)
Dabs(x)
Attempts:
3 left
💡 Hint
Common Mistakes
Using abs() which calls the function immediately.
Trying to use abs(x) directly without a lambda.
4fill in blank
hard

Fill 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'
Alambda x: x > 3
Blambda x: x < 3
Clambda x: x**2
Dlambda x: x*2
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong filter condition like x < 3.
Using map function that doubles instead of squares.
5fill in blank
hard

Fill 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'
Aword
Blen(word)
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names between filter and comprehension.
Using wrong expressions for keys or values.