Finding the items in a list by comparing with two string inputs

I have two string data like below

text1 = "hello how are you"
text2 = "hello how is professional life"

Then also I have a list of items which can/cannot be present in two strings like

test_data = ["are","how", "you","test","test2"]

[word for word in test_data if word not in text1] Using this I will get items in test_data which is not present in text1. Here the output obtained is

['are', 'you', 'test', 'test2']

In this test and test2 are not present in first and second string too. So I dont expect that in the result. So the expected result is

['are', 'you'] 

I am trying to find the data which is present only in text1 and not in text2

>Solution :

Just extend your approach by a second condition:

[word for word in test_data if word  in text1 and word not in text2]

Leave a Reply