Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading