0
0
Pythonprogramming~10 mins

map() function in 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 str function to each number in the list using map().

Python
numbers = [1, 2, 3]
result = list(map([1], numbers))
print(result)
Drag options to blanks, or click blank then click option'
Alen
Bint
Csum
Dstr
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using int instead of str which does not convert numbers to strings.
Using sum which expects an iterable, not a single value.
Using len which returns length, not conversion.
2fill in blank
medium

Complete the code to square each number in the list using map() and a lambda function.

Python
numbers = [1, 2, 3]
squared = list(map(lambda x: x[1]2, numbers))
print(squared)
Drag options to blanks, or click blank then click option'
A+
B**
C*
D-
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using * without repeating the variable (like x*x inside lambda).
Using + which adds instead of squares.
Using - which subtracts.
3fill in blank
hard

Fix the error in the code to convert all strings in the list to uppercase using map().

Python
words = ['apple', 'banana', 'cherry']
upper_words = list(map([1], words))
print(upper_words)
Drag options to blanks, or click blank then click option'
Astr.upper
Bstr.upper()
Cupper
Dstr.uppercase
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using str.upper() which calls the method immediately and causes an error.
Using upper which is undefined.
Using str.uppercase which does not exist.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values using map().

Python
words = ['cat', 'dog', 'bird']
lengths = dict(zip(words, map([1], [2])))
print(lengths)
Drag options to blanks, or click blank then click option'
Alen
Bwords
Crange(len(words))
Dstr
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using range(len(words)) which gives indices, not words.
Using str which converts to string, not length.
Mapping over something other than the words list.
5fill in blank
hard

Fill all three blanks to filter and create a dictionary of words longer than 3 characters with their uppercase forms using map() and filter().

Python
words = ['cat', 'dog', 'bird', 'elephant']
filtered = filter(lambda w: len(w) [1] 3, words)
result = {w[2]: [3](w) for w in filtered}
print(result)
Drag options to blanks, or click blank then click option'
A>
B==
C.upper()
Dstr.upper
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using == instead of > in filter condition.
Using upper without calling it as a method.
Using str.upper incorrectly without parentheses.