Your code makes 24 functions calls and it takes 0.037619808997988 seconds to run it 3500 times.
However, the code below makes 16 function calls and it takes 0.013617821998195723 seconds to run it 3500 times.
So the code below is about two and a half times faster than your code.
qo = ['10,2,1,100,20,200,28,29,30,', '10,2,1,100,20,200,28,29,30,', '10,2,1,100,20,200,28,29,30,']
for i,j in enumerate(qo):
#Transform string to list
*mylist, = qo[i]
for k,l in enumerate(mylist):
if mylist[k] == '2' and mylist[k+1] == '8' and mylist[k+2] == ',':
#Transform in string again
qo[i] = ''.join(mylist)
I achieved this by:
- Using less temporary variables.
- Not converting the strings into integers. If I'm not going to deal
with them as numbers and do arithmetics over them, this is not
- Using .pop() instead of .remove(). I am removing items by their
index. It may be tricky to understand what is happening here at a
first sight but it gets easier if you remember that the item at the
k-numbered index changes after each .pop() call. After all three
calls, we sucessfully remove ['2','8',','] from our list.