MyPyTutor ProblemsOnline Tools Preferences Feedback Help List Comprehension List
ID: 3599584 • Letter: M
Question
MyPyTutor ProblemsOnline Tools Preferences Feedback Help List Comprehension List comprehension is typically used to filter and transform lists Write the function square_odds that takes a list of integers nums and returns the squares of the odd numbers in the list nums. You must use list comprehension. Write square_odds(nums) Submitted: No square_odds ([])- [] square_odds ([1,2,3,4,5]) -> [1,9,25] square_odds ([2,4,8]) -> [.] name 'square_odds' is not defined square_odds([7,3,5]) - [49,9,25] You need to define square_oddsExplanation / Answer
def square_odds(nums):
# return square of x for each x in nums and x is odd.
return [x**2 for x in nums if x%2!=0]
print(square_odds([2,4,8]))
print(square_odds([1,2,3,4,5]))
"""
sample output
[]
[1, 9, 25]
"""
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.