Which of the following is not a valid list comprehension?

  • [x for x in range(10)]
  • [(x, x**2) for x in y]
  • [x if x % 2 == 0 else 'odd' for x in range(10)]
  • [x if x > 5 else for x in range(10)]
The fourth option is not a valid list comprehension because it's missing the expression that should follow the else keyword. In list comprehensions, the else part should have an expression to be evaluated when the condition is False. The correct form should be like: [x if x > 5 else for x in range(10)].
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *