Skip to content

How to filter chains by given predicate (Python)? #97

Closed Answered by wojdyr
hlvlad asked this question in Q&A
Discussion options

You must be logged in to vote

It's similar as it would be with deleting items from a list. Enumerating and deleting at the same time won't work.
But it works when going backward. You can either iterate backward (I'm using here example from SO):

for i in range(len(somelist) - 1, -1, -1):
    if some_condition(somelist, i):
        del somelist[i]

or you can first make a list of items that are to be deleted, and then delete them in reversed order:

to_be_deleted = [n for n, chain in enumerate(model) if should_chain_be_deleted(chain)]
for index in reversed(to_be_deleted):
    del model[index]

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@wojdyr
Comment options

@hlvlad
Comment options

@wojdyr
Comment options

@hlvlad
Comment options

@wojdyr
Comment options

Answer selected by hlvlad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants