I want to remove an element from list, such that the element contains \'X\'
or \'N\'
. I have to apply for a large genome. Here is an example:
For basis purpose
>>> [x for x in ['AAT','XAC','ANT','TTA'] if "X" not in x and "N" not in x]
['AAT', 'TTA']
But if you have huge amount of data, I suggest you to use dict or set
And If you have many characters other than X and N, you may do like this
>>> [x for x in ['AAT','XAC','ANT','TTA'] if not any(ch for ch in list(x) if ch in ["X","N","Y","Z","K","J"])]
['AAT', 'TTA']
NOTE: list(x)
can be just x
, and ["X","N","Y","Z","K","J"]
can be just "XNYZKJ"
, and refer gnibbler answer, He did the best one.