1

I have

x = 1

list = [1,2,3]

I want to remove the 1 in the list by referencing its value of x,not the index - without using list.remove(x). How is this possible?

3
  • What's wrong with remove? Commented Oct 30, 2014 at 3:35
  • Nothing, I'm just wondering if there is another way in python to accomplish this. Commented Oct 30, 2014 at 3:36
  • 3
    Its Python you are using. Don't have to re-invent wheels. Commented Oct 30, 2014 at 3:38

3 Answers 3

1

Indeed, there is a lot of way to do that.

x = 5
my_list = [1, 2, 3, 5, 6, 9, 4]

del my_list[my_list.index(x)]

or

my_list.pop(my_list.index(x))
Sign up to request clarification or add additional context in comments.

3 Comments

But what if I don't know the index of x, just the value?
But this code do exactly that! You can change x to any value you want.
Ohh! Sorry I'm sorda new to Python, it took me a second to fully understand your code. Thanks!
1
x = 1
list = [1,2,3]
list = [i for i in list if i != x]

Comments

0
list = [1,2,3]
list = [i for i in list if i is not 1]

1 Comment

OP says "I want to remove the 1 in the list by referencing its value of x", so it seems the code ought to use x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.