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
  • 5,668
  • 14
  • 46
  • 94

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
  • 225,348
  • 35
  • 301
  • 374
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,095
  • 1
  • 19
  • 33