Certified Associate in Python Programming (PCAP-31-03) — Question 21

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)

Answer options

Correct answer: C

Explanation

The correct answer is C because the list comprehension iterates through the indices 4 to 1 and includes only the odd numbers from my_list, which are 3 and 1. Option A is incorrect because the code is valid and will execute. Option B is wrong as it suggests the output is even numbers, and D is incorrect because it reverses the order of the odd numbers.