Reputation: 2393
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']
Upvotes: 0
Views: 44
Reputation: 11134
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']
Upvotes: 2
Reputation: 2253
>>> 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']
>>>
Upvotes: 0