LearnFrenzy
Sign in Take a free test

List Comprehensions · List Comprehensions MCQ · Question 3

Where does if-else go? Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links