Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.โ Incorrect
The
map() function applies the given function to each item in the list. Here, str converts numbers to strings.2fill in blank
mediumComplete 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'
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.โ Incorrect
The lambda function squares each number using the exponent operator
**.3fill in blank
hardFix 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'
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.โ Incorrect
Use
str.upper without parentheses to pass the method itself to map().4fill in blank
hardFill 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'
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.
โ Incorrect
Use
len to get length of each word and map it over the words list.5fill in blank
hardFill 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'
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.โ Incorrect
Filter words longer than 3 using
>, then create keys as words with uppercase values using str.upper method.