Define a function called purify that takes in a list of numbers, removes all odd numbers in the list, and returns the result.

For example, purify([1,2,3]) should return [2].

Why this code is not working? For example, input [4,5,5,4] will return [4,5,4]

def purify(numbers):
    for i in numbers:
        if i % 2 == 1:
    return numbers


I would personally append the even numbers to a new list

The reason remove is not working, is because of index shift.

if you have 4,5,5,4 the items are indexed at 0,1,2,3. Agree? when the for loop arrives at index 1, it finds 5, and it removes it. But here is the catch, the loop continues to index 2 but all items are going to shift. The 5 at index 2 will shift to index 1 to take the place of the removed number. Oops, problem: the for loop is now at index 2, but the second 5 shifted to index 1, which means it gets skipped

Can you solve this? Yes, you could use a while loop to remove any succession odd numbers, which will inevitable lead to a index error, so you will need to catch it (try except)

But that is difficult, i would just append even numbers to a new list


what u done is perfectly right but the instructions has clearly say that the input list should not be modified, like in ur case. Instead u can append the result to new list such as

def purify(seq):
new_seq = []
for i in range(len(seq)):
if seq[i]%2 == 0:
return new_seq


Agree, the instruction do say this. But i think this is purely because the only the way to modify the input list is by using remove, which is tedious. But i agree, it would be much better to append to a new list.


Whenever we write a function, we should carefully specify the purpose of the function, as well as any interaction that the function should have with the software environment in which it is used. The purpose of many, but certainly not all, functions is to produce an object, for example a single value or a list, and return it to where the function was called. If a function modifies an object such as a global variable or an argument that is passed to it, this is an example of a side effect. Side effects that are not carefully planned and documented are a common source of bugs.

The purpose of the purify function is to take in a list of numbers, remove all odd numbers in the list, and return the result. This purpose does not include modifying the original list. Therefore, if a user of the function passes a list to it, it might become an unwelcome surprise for the user to find out later, that the original list had been modified. It is quite possible that the user will still want access to the list in its original form, and the unexpected side effect will constitute a bug.

As @stetim94 correctly pointed out, using the remove method on the original list to produce the result is not a good way to go because it causes the indexes to shift during the loop. But, another thing to keep in mind is that unless we are told to write a function that has specific side effects, we should avoid causing those side effects. In the case of the purify function, we should leave the original list in the state that it was in when it was passed to the function.

The built in Python sorted function is an example of one that does not have a side effect. It leaves the list that was passed to it as is, and returns a new sorted list.

In contrast, the Python list sort method has the effect of sorting the original list that it is called upon. That is great, because this useful effect is the purpose of the method, and is meticulously documented.


Thank you all for replying. The comments are really helpful for me. Learned so much!

I totally agree append is a better way to solve this practice.

`def purify(numbers):
    result = []
    for i in numbers:
	    if i % 2 == 0:
    return result`


Do not remove data from numbers list