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

Drop the value that matches the string from list in python

I have a list that contains strings. I want to drop out the ones that have specific strings using python.

For example:

my_sample_list = ["I am practicing Python", "I am practicing Java", "I am practicing SQL"]

I want to drop out the element that contains "SQL" and I will be left with:

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

my_new_sample_list = ["I am practicing Python", "I am practicing Java"]

How can I do that in python, please? Any help would be appreciated.

>Solution :

Iterate over the strings and check if the pattern is contained in it or not.

my_sample_list = ["I am practicing Python", "I am practicing Java", "I am practicing SQL"]

pattern = 'SQL'
my_new_sample_list = [s for s in my_sample_list if pattern not in s]
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