0

Newbie question here...

I have a list of lists and I need to filter out entire elements (rows) if one occurrence of a partial string in a row exists.

E.g.

df = [['frog','womble','badger'], ['frog','ant','owl'], ['frog','badger','dataERRORdata']]

I need to search on %ERROR% (because the surrounding text is large and changeable) and for that to remove the entire element, leaving;

[['frog','womble','badger'], ['frog','ant','owl']]

I've tried a few list comprehensive/generator type loops but can't get it work. Would be quite useful to have an example just based on the above so I can tweak it to the specific case I have.

Feels like this is simple and I'm just missing something easy

Thanks!

1
  • 1
    check the in operator Commented Mar 30, 2020 at 16:41

2 Answers 2

3

You can use a generator expression within all to check each sublist for a string containing 'ERROR', the use a list comprehension to loop over those sublists to filter.

>>> [sub for sub in df if all('ERROR' not in s for s in sub)]
[['frog', 'womble', 'badger'], ['frog', 'ant', 'owl']]
Sign up to request clarification or add additional context in comments.

Comments

3

This comprehension with any will do:

df = [['frog','womble','badger'],['frog','ant','owl'],['frog','badger','dataERRORdata']]

clean = [sub for sub in df if not any('ERROR' in w for w in sub)]
# [['frog', 'womble', 'badger'], ['frog', 'ant', 'owl']]

This is short-hand for the following nested loop (docs on for-else):

clean = []
for sub in df:
    for w in sub:
        if 'ERROR' in w:
            break
    else:  # loop not broken out of
        clean.append(sub)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.