0

Here is the code I tried:

def filter_list(lst):
    for l in lst:
        print(l)
        if isinstance(l, str):
            lst.remove(l)
    return lst
print(filter_list([1, 2, "aasf", "1", "123", 123]))

I recieved following output:

C:\Users\acain\PycharmProjects\gfn\venv\Scripts\python.exe C:/Users/acain/PycharmProjects/gfn/experiment.py
1
2
aasf
123
[1, 2, '1', 123]

Process finished with exit code 0  

I can't understand why strings "1" and "123" are not printed. Please help me.

mig001
  • 179
  • 1
  • 18
  • 7
    Do ***NOT*** delete from the same list that you are iterating over – rdas Apr 30 '20 at 14:23
  • 4
    Mutating the list while iterating through it is the issue. **THUMB RULE**: Never mutate while iterating through it. – Ch3steR Apr 30 '20 at 14:23
  • when you are remove the current list the size of the list change – Beny Gj Apr 30 '20 at 14:23
  • 1
    try creating a new list containing only the elements you want – AbbeGijly Apr 30 '20 at 14:26
  • @BenyGj Simply changing the size of the list is not an issue; *appending* to the list, for example, would not cause a problem (though if you *always* append to the list on each iteration, the loop would never terminate). – chepner Apr 30 '20 at 14:27
  • Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Torxed Apr 30 '20 at 14:28
  • Why loop terminate somewhere when remove is used? Why it is not happened with append? I didn't get that – mig001 Apr 30 '20 at 14:34

2 Answers2

0

it is not printing "1" and "123" as you are printing and using in instance in the same for loop, i.e. for l in lst: print(l)

it will print all the items but then in this loop you are using isinstance(l,str). which is removing the items. try this code:-

    def filter_list(lst):
        for l in lst:
            print(l)
        for l in lst:
            if isinstance(l, str):
                lst.remove(l)
        return lst
    print(filter_list([1, 2, "aasf", "1", "123", 123]))

output:- $ python hello2.py

1

2

aasf

1

123

123

[1, 2, '1', 123]

here in this image is the code and the output

I hope this will be useful for u

0

If you want to iterate over a list while modifying it, try this:

def filter_list(lst):
    i = 0
    while i < len(lst):
        print(lst[i])
        if isinstance(lst[i], str):
            lst.remove(i)
        else:
            i += 1
    return lst

This way, each iteration either shortens the list or advances the pointer.

It's worth noting that this is not considered overly "Pythonic". List comprehension would give more compact code, and in many cases, results that are just as good.

Amitai Irron
  • 1,973
  • 1
  • 12
  • 14
  • As the iteration proceeds program came up with following error. (This happens because in each iteration as element of list is removed, length of list changes) '''print(filter_list([1, 2, "aasf", "1", "123", 123])) File "C:/Users/acain/PycharmProjects/gfn/hello world.py", line 6, in filter_list lst.remove(i) ValueError: list.remove(x): x not in list''' – mig001 Apr 30 '20 at 15:47