0

I am trying to Remove list elements(numeric values) while iterating through the list. I have two examples. example 1 works but example 2 doesn't, even though both examples use the same logic.

Example 1 : Working

list1=["5","a","6","c","f","9","r"]
print(list1)
for i in list1:
    if str.isnumeric(i):
    list1.remove(i)
print(list1)

Example 2 : Not Working

list2=["12abc1","45asd"]
for items in list2:
    item_list=list(items)
    print(item_list)
    for i in item_list:
        if str.isnumeric(i):
            item_list.remove(i)
    print(item_list)

I solved the example 2 by using (for i in item_list[:]:). But i can't understand the logic why second example didn't work at first place?

jnth132
  • 3
  • 3

1 Answers1

0

I can't claim to be an expert in Python, as I'm only poorly familiar with it, however I'll give you an explanation of what I think is likely happening.

The first example doesn't actually work any better than the second example, however the data you've used to test it is different so it doesn't show. The problem seems to be due to the fact that you're iterating through any modifying at the same time, so the following happens in the second example:

  • The program will iterate through its given list:

    ["1", "2", "a", "b","c", "1"]

  • The program starts with list item 1. It is numerical, so it is removed. The list is now different:

    ["2", "a", "b", "c", "1"]

  • As you are iterating through, it moves on to list item 2. This is problematic, as list item 2 is "a" rather than the "2", so it skips the "2".

As numbers in the first example are separated by at least 1 list item, this isn't an issue as all of the numbers are iterated over.

As for the fix you mentioned of changing list2 to list2[:], I have no idea what happened there as when I ran the program through PythonTutor's visualizor it didn't seem to work.

In order to fix this, the most obvious solution to me would be to try going through the array backwards - starting with the final list item and moving towards the start of the list, as that means any item you remove won't affect the numbering of the previous items.

Hope I helped!

VortixDev
  • 965
  • 1
  • 10
  • 23
  • Ok my mistake!! it should be (for i in item_list[:]: ). Second loop should be altered not the first one. – jnth132 Jul 18 '16 at 17:45
  • You're right example 1 also not working when consecutive numbers in the list (ex : list1=["5","6","a","7","c","f","9","r"]) – jnth132 Jul 18 '16 at 17:56
  • Oh, right. Running another test with PythonTutor, it seems that putting the [:] in corrects this error. After a search, I've found this answer http://stackoverflow.com/questions/6167238/what-does-in-python-mean which explains that this usage creates a copy of the list, therefore when iterating over it using the for loop it will still iterate over all values as you haven't removed the values from the list copy that is being used, but rather from the original list. This should mean that putting the [:] in example 1 will fix that example. – VortixDev Jul 18 '16 at 18:14