QuestionQ7

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

What behavior should be expected from this code?

my_list = [i for i in range(5)]  
m = [my_list[i] for i in range(5) if my_list[i] % 2 != 0]  
print(m)  
Explanation

The conditional my_list[i] % 2 != 0 keeps only values that are not evenly divisible by 2. From the integers 0 through 4, those values are 1 and 3, so the printed list is [1, 3].

Community Discussion

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