Is it possible to do this example using List Comprehensions:
a = ['test', 'smth']
b = ['test Lorem ipsum dolor sit amet',
'consectetur adipiscing elit',
'test Nulla lectus ligula',
'imperdiet at porttitor quis',
'smth commodo eget tortor',
'Orci varius natoque penatibus et magnis dis parturient montes']
for s in a:
b = [el.replace(s,'') for el in b]
What I want is to delete specific words from list of sentences. I can do it using loop, but I suppose it is possible using some one-line solution.
I tried something like:
b = [[el.replace(s,'') for el in b] for s in a ]
but it goes wrong
I got a lot of quality answers, but now I have on more complication: what if I want to use combination of words?
a = ['test', 'smth commodo']
Thank you for a lot of answers! I made speed test for all the solutions and here is the result: I did it mean of 100 calculations (except the last one, it's too long to wait).
b=10 a=2 | b=9000 a=2 | b=9000 a=100 | b=45k a=500
---------------------------------+-------------+--------------+---------------
COLDSPEED solution: 0.0000206 | 0.0311071 | 0.0943433 | 4.5012770
Jean Fabre solution: 0.0000871 | 0.1722340 | 0.2635452 | 5.2981001
Jpp solution: 0.0000212 | 0.0474531 | 0.0464369 | 0.2450547
Ajax solution: 0.0000334 | 0.0303891 | 0.5262040 | 11.6994496
Daniel solution: 0.0000167 | 0.0162156 | 0.1301132 | 6.9071504
Kasramvd solution: 0.0000120 | 0.0084146 | 0.1704623 | 7.5648351
We can see Jpp solution is the fastest BUT we can't use it - it's the one solution from all others which can't work on combination of words (I already wrote him and hope he will improve his answer!). So looks like the @cᴏʟᴅsᴘᴇᴇᴅ 's solution is the fastest on the big data sets.
pythonicand maybe a little faster.