Creating a function that removes odd multiples in a list

Viewed 97

One of the revision questions involves modifying the original list by removing all integers in a list that are both odd and a multiple of x.

def remove_odd_multiples(numbers_list, multiple_of):
    for ele in numbers_list:
        if (ele%2) != 0 and (ele % multiple_of) == 0:
            numbers_list.remove(ele)
    
    return numbers_list

Output:

numbers_list = [1, 5, 23, 3, 6, 17, 9, 18]
print("Before:", numbers_list)
remove_odd_multiples(numbers_list, 3)
print("After:", numbers_list)

Before: [1, 5, 23, 3, 6, 17, 9, 18]
After: [1, 5, 23, 6, 17, 18]

It does work, however inputting the code into coderunner, my code failed a few hidden checks which are not shown. Am I going about solving this problem wrong? Should I be using pop instead of .remove?

2 Answers

Instead of removing while iterating, you could use a list comprehension to return a new list with the result.

def remove_odd_multiples(numbers_list, multiple_of):
    return [x for x in numbers_list if x % 2 == 0 or x % multiple_of != 0]

To modify the list in-place:

def remove_odd_multiples(numbers_list, multiple_of):
    numbers_list[:] = [x for x in numbers_list if x % 2 == 0 or x % multiple_of != 0]

Looping backwards while deleting elements also ensures that unprocessed elements are not shifted.

def remove_odd_multiples(numbers_list, multiple_of):
    for i in range(len(numbers_list) - 1, -1, -1):
        if (numbers_list[i]%2) != 0 and (numbers_list[i] % multiple_of) == 0:
            del numbers_list[i]

I think the problem is that while iterating the list, you are also modifying it (by deleting the elements of the list).

So, try this:

def remove_odd_multiples(numbers_list, multiple_of):
    for ele in numbers_list[:]:
        if (ele%2) != 0 and (ele % multiple_of) == 0:
            numbers_list.remove(ele)
    
    return numbers_list

For more information - Strange result when removing item from a list while iterating over it

Related