Let's consider the following problem. A list of numbers is given: numbers = [1, 2, 3, 4, 5, 6] Our task is to leave only even numbers in the list. We may be tempted to write the following code: for i in numbers: if i % 2 != 0: del numbers[i] This solution is incorrect because we iterate over the object from which we remove elements (this causes some element skips in the iterable object and results in a wrong answer). Select the obtained numbers list.

Questions & AnswersCategory: PythonLet's consider the following problem. A list of numbers is given: numbers = [1, 2, 3, 4, 5, 6] Our task is to leave only even numbers in the list. We may be tempted to write the following code: for i in numbers: if i % 2 != 0: del numbers[i] This solution is incorrect because we iterate over the object from which we remove elements (this causes some element skips in the iterable object and results in a wrong answer). Select the obtained numbers list.
Lokesh Kumar Staff asked 2 years ago

Let’s consider the following problem. A list of numbers is given:

numbers = [1, 2, 3, 4, 5, 6]

Our task is to leave only even numbers in the list. We may be tempted to write the following code:

 

for i in numbers:

    if i % 2 != 0:

        del numbers[i]

This solution is incorrect because we iterate over the object from which we remove elements (this causes some element skips in the iterable object and results in a wrong answer).
Select the obtained numbers list.
a. []
b. [1, 3, 5]
c. [1, 3, 4, 6]
d. [2, 4, 6]