How to remove item from a python list in a loop? [duplicate]

You can’t remove items from a list while iterating over it. It’s much easier to build a new list based on the old one:

y = [s for s in x if len(s) == 2]

Leave a Comment