Complete the code to filter out even numbers from the list.
numbers = [1, 2, 3, 4, 5, 6] even_numbers = list(filter(lambda x: x [1] 2 == 0, numbers)) print(even_numbers)
The modulo operator % returns the remainder. Using x % 2 == 0 checks if a number is even.
Complete the code to filter words longer than 4 characters.
words = ['apple', 'bat', 'carrot', 'dog'] long_words = list(filter(lambda word: len(word) [1] 4, words)) print(long_words)
We want words longer than 4 characters, so we use len(word) > 4.
Fix the error in the lambda to filter numbers less than 10.
nums = [5, 12, 7, 20, 3] small_nums = list(filter(lambda n: n [1] 10, nums)) print(small_nums)
To filter numbers less than 10, use n < 10.
Fill both blanks to filter words starting with letter 'a' and having length less than 5.
words = ['apple', 'ant', 'bat', 'arc', 'dog'] filtered = list(filter(lambda w: w[1] 'a' and len(w) [2] 5, words)) print(filtered)
Use w.startswith('a') to check the first letter and len(w) < 5 for length less than 5.
Fill both blanks to create a dictionary of words and their lengths, filtering words longer than 3 characters.
words = ['sun', 'moon', 'star', 'sky'] result = {w:: len(w) [1] w in words if len(w) [2] 3} print(result)
The dictionary comprehension syntax is {key: value for item in iterable if condition}. Here, w: len(w), for w in words, and len(w) > 3.