0

Hi I have the following 2 lists and I want to get a 3rd updated list basically such that if any of the strings from the list 'wrong' appears in the list 'old' it filters out that entire line of string containing it. ie I want the updated list to be equivalent to the 'new' list.

wrong = ['top up','national call']
old = ['Hi Whats with ','hola man top up','binga dingo','on a national call']
new = ['Hi Whats with', 'binga dingo']

2 Answers 2

2

You can use filter:

>>> list(filter(lambda x:not any(w in x for w in wrong), old))
['Hi Whats with ', 'binga dingo']

Or, a list comprehension,

>>> [i for i in old if not any(x in i for x in wrong)]
['Hi Whats with ', 'binga dingo']

If you're not comfortable with any of those, use a simple for loop based solution like below:

>>> result = []
>>> for i in old:
...     for x in wrong:
...         if x in i:
...             break
...     else: 
...         result.append(i)
... 
>>> result
['Hi Whats with ', 'binga dingo']
Sign up to request clarification or add additional context in comments.

Comments

0
>>> wrong = ['top up','national call']
>>> old = ['Hi Whats with ','hola man top up','binga dingo','on a national call']
>>> [i for i in old if all(x not in i for x in wrong)]
['Hi Whats with ', 'binga dingo']
>>> 

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.