Skip to content
Chapter Practice Arena

Comprehensions and Functional Patterns Problems

Targeted practice challenges specifically curated for this chapter. Inspect hints, verify outputs, and check Pythonic solutions.

All Chapters
Difficulty:
Chapter:
Showing 6 challenges · 25 total · Page 1 of 5
Given words = ['cat', 'dog', 'elephant'], create a dictionary using each word's length as the key and the word itself as the value. Print the result. Explain why the key 3 maps to only one word.
Starter Code
words = ['cat', 'dog', 'elephant']
# Build and print the dictionary
Expected Output:{3: 'dog', 8: 'elephant'}
#2intermediateIterators and Generators
Write a generator positive_numbers(numbers) that yields only numbers greater than zero. Use it with sum() to calculate the total of [-3, 4, 0, 7, -1].
Starter Code
def positive_numbers(numbers):
    # Yield only positive values
    pass

values = [-3, 4, 0, 7, -1]
print(sum(positive_numbers(values)))
Expected Output:11
Given numbers = [-3, 1, 2, 4, -5], create a list containing the squares of only the positive numbers. Print the result.
Starter Code
numbers = [-3, 1, 2, 4, -5]
# Keep positive numbers and square them
Expected Output:[1, 4, 16]
#4beginnerList Comprehensions
Use a list comprehension to create a list containing double each integer from 1 through 3. Print the resulting list.
Starter Code
# Create the list using a comprehension
Expected Output:[2, 4, 6]
Given scores = {'Asha': 75, 'Ravi': 42, 'Mina': 90, 'Kabir': 49}, create a dictionary containing only students whose scores are at least 50. Preserve each student's name and score.
Starter Code
scores = {'Asha': 75, 'Ravi': 42, 'Mina': 90, 'Kabir': 49}
# Create and print the passing scores
Expected Output:{'Asha': 75, 'Mina': 90}
#6intermediateLambda, map, and filter
Given numbers = [1, 2, 3, 4, 5, 6], select the even numbers and then double the selected values. Use filter() and map(), and print the final list.
Starter Code
numbers = [1, 2, 3, 4, 5, 6]
# Filter even numbers, then double them
Expected Output:[4, 8, 12]
…