QuestionQ8

Miscellaneous (List Comprehensions, Lambdas, Closures, I/O)

What is the expected behavior of the following code?

my_list = [i for i in range(5)]  
m = [my_list[i] for i in range(4, 0, -1) if my_list[i] % 2 != 0] print(m)  
  • A the code is erroneus and it will not execute
  • B it outputs [4, 2, 0]
  • C it outputs [3, 1]
  • D it outputs [1, 3]
Explanation

Python statements must be separated by a newline or a semicolon. Placing print(m) directly after the closing bracket of the list-comprehension assignment produces a syntax error, so the code does not execute.

Community Discussion

No comments yet. Be the first to start the discussion!