-5

I have a list, which is a split sentence. For instance:

['Hello', 'My', 'name', 'is', 'Python', 'and', 'I',
 'make', 'life', 'easy', 'is', 'and', 'I', 'is']

I want to remove all occurrences of: 'is', 'and' and 'I', in a single go.

How do I do this in the shortest way possible, by taking out them all in one go? And what if I wanted to take out all occurrences of 5 variables in one go? or 1000 in one go?

I get this is probably super hard. It hasn't been asked before....

  • 1
    No, because I want to remove all occurrences of many variables in one go. – user8173695 Jun 21 '17 at 17:12
  • A list comprehension *does* remove all occurrences in one go. And they're optimized. – alkasm Jun 21 '17 at 17:14
  • Yes, because you can/should be able to easily expand the accepted answer of the duplicate question to check for multiple things. – martineau Jun 21 '17 at 17:15
  • @martineau nowhere in the dup target does it do string comparisons, especially optimized via sets. All of the answers relate to integers. Nor do any of them remove more than one value. Especially for new programmers, "you should be able to easily expand..." probably doesn't hold. The question title may be poorly worded, and there is a better [dupe target](https://stackoverflow.com/questions/10563288/python-remove-all-occurrences-of-string-in-list), but I don't think that one answers this question. – TemporalWolf Jun 21 '17 at 17:30
  • 1
    @TemporalWolf: stackoverflow can be a harsh mistress. – martineau Jun 21 '17 at 17:40

1 Answers1

1
In [150]: L = ['Hello', 'My', 'name', 'is', 'Python', 'and', 'I', 'make', 'life', 'easy', 'is', 'and', 'I', 'is']

In [151]: blacklist = {"I", "is", "and"}

In [152]: [i for i in L if i not in blacklist]
Out[152]: ['Hello', 'My', 'name', 'Python', 'make', 'life', 'easy']
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241