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

Removing items from list if they contain a substring

Let’s say I have a list that looks like this:

'CALSIM', '1556', '1897', '1358', '1321', '1254', '1403', '1124', '2561', '958', '712', '704', '562', '2092', '2020', '2001', '1890', '1748', 'Sky1', '1307', 'Sky29', '1802', '3429', '3522', 'Sky21'

I want to remove all instances of CALSIM and all instances of Sky# from my list. Is there a convenient method in Python?

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

>Solution :

You can add more conditions if you need.

strings = ['CALSIM', '1556', '1897', '1358', '1321', '1254', '1403', '1124', '2561', '958', '712', '704', '562', '2092', '2020', '2001', '1890', '1748', 'Sky1', '1307', 'Sky29', '1802', '3429', '3522', 'Sky21']
strings = [row for row in strings if row != "CALSIM" and not row.startswith("Sky")]

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