-1

I have tried execute this, but it dosn't work properly. My goal was to remove all numbers divided by 2. Could someone advise what is wrong. I really do not understand why '4', '8' are still there.

list = [2,4,9,0,4,6,8,3,43,44]
for e in list:
    if e%2==0:
        list.remove(e)
        print(list)
Gytree
  • 542
  • 3
  • 13
  • 2
    Possible duplicate of [How to remove list elements in a for loop in Python?](https://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python) – Sayse Feb 27 '19 at 14:30
  • 2
    Possible duplicate of [strange result when removing item from a list](https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list) – Paritosh Singh Feb 27 '19 at 14:30

4 Answers4

3

You can use a list comprehension to generate a new list with only elements you want to keep.

newList = [x for x in oldList if not isEven(x)]

where the function isEven does something like:

def isEven(target):
    return target % 2 == 0

By the way, your question is a duplicate of the following How to remove items from a list while iterating?

Thomas Milox
  • 96
  • 1
  • 4
1

You can try to use list.pop() with the position of the element you want to remove. The '2' and '4' are still there because they are skipped when you remove the number before them (When you remove the '2', the '4' is moved to the previous position)

Philippe
  • 71
  • 3
  • Quick comment for completeness. `list.pop(index)` removes the element from the list but also returns it. Using `del list[index]` would just remove it. – ingofreyer Feb 27 '19 at 14:50
1

Try this:

l = [2, 3, 4, 5, 9, 10,30,45]
new=[el for el in l if el % 2]
print(new)

Actually, when you remove an element from the list, the indexing gets changed. So, you can do this list comprehension. Also you can use:

l = [2, 3, 4, 5, 9, 10,30,45]
new=[filter(lambda x: x % 2, l)]
print(new)
Faizan Khan
  • 583
  • 1
  • 6
  • 22
1

If you want to keep the list instead of creating a new one (the answer by Thomas Milox is a good one otherwise), you should iterate backward through the list by index. When you remove an element from a list while iterating forwards through the list you may jump over some elements, not processing them. Going backward ensures that no list element removals move any elements that you may still want to process.

Here is an example of how this may look for your code:

list = [2, 4, 9, 0, 4, 6, 8, 3, 43, 44]
for i in range(len(list) - 1, -1, -1):  # start at the last element, go until the first one (index 0 - the last value in the range method will not be reached), go backwards
    if list[i] % 2 == 0:
        del list[i]

You can read a bit more about removing an element by index instead of by value here. This is required since you would otherwise mutate the list on the wrong position for duplicate values. It may also be a bit faster, since removeneeds to iterate through the list, searching for the element to remove while del list[i] may look up the element that needs to be removed by index.

Iterating backward through a list is also covered here.

ingofreyer
  • 1,086
  • 15
  • 27