Question 3
Back to Exercise
Which comprehension replaces negative numbers by 0 and keeps the others?
- A [n if n >= 0 else 0 for n in nums] Correct
- B [n for n in nums if n >= 0 else 0]
- C [n for n in nums if n >= 0]
- D [for n in nums: n if n >= 0 else 0]
Correct Answer: Option A
A conditional expression that chooses each value goes before for. The filter form would drop the negatives instead.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!