I have a list,
numbers=["one","two","three","one","one"]
when I am doing,
numbers.remove("one")
it returns
["two","three","one","one"]
but I want to remove it completely,
My expected output is,
output=["two","three"]
I have a list,
numbers=["one","two","three","one","one"]
when I am doing,
numbers.remove("one")
it returns
["two","three","one","one"]
but I want to remove it completely,
My expected output is,
output=["two","three"]
Try this:
numbers = ["one", "two", "three", "one", "one"]
[element for element in numbers if element != "one"]
=> ["two", "three"]
We used a list comprehension for doing the job, check the link for details. But basically - we created a new list, filtering out the undesired element via a condition.
How about a list comprehension
[i for i in numbers if i!="one"]
You can also use a filter
(Python 2.x)
filter(lambda x: x!="one",numbers)
Output:
['two', 'three']