0
0
Pythonprogramming~10 mins

Lambda with filter() 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 filter out even numbers from the list.

Python
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x [1] 2 == 0, numbers))
print(even_numbers)
Drag options to blanks, or click blank then click option'
A**
B//
C%
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using integer division '//' instead of modulo '%'.
Using exponent '**' or addition '+' operators incorrectly.
2fill in blank
medium

Complete the code to filter words longer than 4 characters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
long_words = list(filter(lambda word: len(word) [1] 4, words))
print(long_words)
Drag options to blanks, or click blank then click option'
A<
B>
C==
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' which filters shorter words.
Using '==' which filters words exactly 4 characters long.
3fill in blank
hard

Fix the error in the lambda to filter numbers less than 10.

Python
nums = [5, 12, 7, 20, 3]
small_nums = list(filter(lambda n: n [1] 10, nums))
print(small_nums)
Drag options to blanks, or click blank then click option'
A<=
B>
C==
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '>' which filters numbers greater than 10.
Using '<=' which includes 10, but we want strictly less.
4fill in blank
hard

Fill both blanks to filter words starting with letter 'a' and having length less than 5.

Python
words = ['apple', 'ant', 'bat', 'arc', 'dog']
filtered = list(filter(lambda w: w[1] 'a' and len(w) [2] 5, words))
print(filtered)
Drag options to blanks, or click blank then click option'
Astartswith(
B==
C<
D>
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '==' to compare the whole word instead of checking start.
Using '>' instead of '<' for length comparison.
5fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths, filtering words longer than 3 characters.

Python
words = ['sun', 'moon', 'star', 'sky']
result = {w:: len(w) [1] w in words if len(w) [2] 3}
print(result)
Drag options to blanks, or click blank then click option'
Bfor
C>
D:
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Missing colon ':' between key and value.
Using incorrect loop keyword.
Using '<' instead of '>' for filtering.