Emilia Clarke
Emilia Clarke

Reputation: 83

How to delete a value in a list without using .remove?

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?

Upvotes: 1

Views: 109

Answers (3)

user3379283
user3379283

Reputation: 11

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

Upvotes: 0

Mauro Baraldi
Mauro Baraldi

Reputation: 6575

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))

Upvotes: 1

dhilipsiva
dhilipsiva

Reputation: 3718

x = 1
list = [1,2,3]
list = [i for i in list if i != x]

Upvotes: 1

Related Questions