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)].
Loading...
Related Quiz
- Which of the following loop types executes at least once, irrespective of the test condition?
- The _____ method in a metaclass is executed when a new class is created.
- How can you reload a module that has been modified after it was initially imported?
- How can you parameterize a test function in pytest to run it multiple times with different arguments?
- How would you use a mock object in Python for testing a function that makes an HTTP request?