I have a list like this.
a = ['\n', 'a', 'b', '\n', 'c', '\n']
As far as I know, the remove, pop, delete method only removes one by one.
If I want to remove all'\n' from this list, the only way to do this is to use remove via a for or a while loop using remove method? Or is there a specific method?
Asked
Active
Viewed 53 times
-1

AuccKhan
- 1
- 2
-
list comprehension `a = [x for x in a if x != '\n']` is your friend – Jean-François Fabre Aug 28 '20 at 13:25
-
@Jean-FrançoisFabre Not really. There may be other references to the list. – alani Aug 28 '20 at 13:25
-
2okay then: `a[:] = [x for x in a if x != '\n']` (but OP didn't specify that part) – Jean-François Fabre Aug 28 '20 at 13:26
1 Answers
0
How about:
a = ['\n', 'a', 'b', '\n', 'c', '\n']
list(filter(('\n').__ne__, a))

Raymond Reddington
- 1,709
- 1
- 13
- 21
-
-
but it's covered already here: https://stackoverflow.com/a/1157160/6451573. I would be cool if people voted to close instead of FFGITW answering. And list comprehensions are generally used, not this clunky list+filter+__ne__ stuff – Jean-François Fabre Aug 28 '20 at 13:30