How to strip/remove certain characters from a list

Question:

I am currently trying to remove certain characters recursively from a python 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

>>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’.

Asked By: Pythonstudent

||

Answers:

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)
Answered By: Hanno Reuvers

The problem with your code right now is that you are trying to strip the list, not the attribute in the list. I encourage you to do more research on for loops. In the future, search on google, look at documentation, or look for other similar questions before asking your own.

The split method for strings is used for removing characters from the front and the rear of the string. In this situation, you want to remove specific characters from the middle of the string. To do this, we will use the string.replace(characterstoreplace, whattoreplaceitwith) method.

The resulting code should look like this. Please try to understand it for yourself rather than just copy pasting it. You can ask me if you have any questions.

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

for i in range(len(lst1)): #goes through every attribute in the list, represented by i
        lst1[i] = lst1[i].replace("_out", "") #replaces '_out' with nothing and sets the new value
Answered By: Larg Ank

You can use map to remove the strings too if you don’t want to do list comprehension.

list(map(lambda x:x.replace('_out',''),lst1))
Out[136]: ['ZINC1.pdbqt', 'ZINC2.pdbqt', 'ZINC3.pdbqt']

To see the performance, let’s try with 30000 components in the strings.

# Appending to list (From @Hanno Reuvers)
%timeit append_list_function()
8.68 ms ± 236 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# List comprehension
%timeit [s.replace('_out', '') for s in newlist]
7.06 ms ± 361 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# Map function
%timeit list(map(lambda x:x.replace('_out',''),newlist))
8.69 ms ± 137 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.