Complete the code to filter out even numbers from the list.
numbers = [1, 2, 3, 4, 5] even_numbers = list(filter([1], numbers)) print(even_numbers)
The filter() function needs a function that returns True for even numbers. lambda x: x % 2 == 0 does exactly that.
Complete the code to filter words longer than 3 characters.
words = ['cat', 'lion', 'dog', 'elephant'] long_words = list(filter([1], words)) print(long_words)
< 3 instead of > 3.The filter function should keep words with length greater than 3, so lambda w: len(w) > 3 is correct.
Fix the error in the filter function to keep only positive numbers.
numbers = [-2, 0, 3, 5, -1] positive = list(filter([1], numbers)) print(positive)
To keep only positive numbers (greater than zero), use lambda x: x > 0. Using x >= 0 would include zero, which is not positive.
Fill both blanks to filter words starting with 'a' and convert them to uppercase.
words = ['apple', 'banana', 'avocado', 'cherry'] filtered = list(filter([1], words)) result = [w.[2]() for w in filtered] print(result)
endswith instead of startswith.lower() instead of upper().The filter keeps words starting with 'a' using lambda w: w.startswith('a'). Then, upper() converts them to uppercase.
Fill all three blanks to filter numbers divisible by 3, square them, and keep only those greater than 10.
numbers = [1, 3, 4, 6, 9, 12] filtered = filter([1], numbers) squared = map(lambda x: x[2]2, filtered) result = list(filter(lambda x: x [3] 10, squared)) print(result)
< instead of > in the last filter.First, filter numbers divisible by 3 with lambda x: x % 3 == 0. Then square them using ** 2. Finally, keep only squares greater than 10 with > 10.