0

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"]
Pyd
  • 6,017
  • 18
  • 52
  • 109

2 Answers2

1

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

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']
Miraj50
  • 4,257
  • 1
  • 21
  • 34