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.