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

How to strip/remove certain characters from a lsit

I am currently trying to remove certain characters recursively from a list.

I have a list:

lst1 = ['ZINC1_out.pdbqt', 'ZINC2_out.pdbqt', 'ZINC3_out.pdbqt']

I would like to remove the ‘_out’ so that the list looks like this

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

>>lst1
['ZINC1.pdbqt', 'ZINC2.pdbqt', 'ZINC3.pdbqt']

My current code looks like this:

lst1 = ['ZINC1_out.pdbqt', 'ZINC2_out.pdbqt', 'ZINC3_out.pdbqt']    
for i in lst1:
        lst1.strip("_out")

But I get an error: AttributeError: ‘list’ object has no attribute ‘strip’.

Any help would be really appreciated.

>Solution :

The following code achieves your goal. As already suggested by @drum, the replace method is useful for this.

lst1 = ['ZINC1_out.pdbqt', 'ZINC2_out.pdbqt', 'ZINC3_out.pdbqt']
lst2 = []
for i in lst1:
    lst2.append( i.replace("_out", "") )

print(lst2)
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