Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

remove item from list if it exists python

# 1) Almost-English style:
if thing in some_list:
	some_list.remove(thing)
    
# 2) Duck type, EAFP style:
with suppress(ValueError, AttributeError):
    some_list.remove(thing)
    
# 3) Filter it out
some_list = filter(lambda x: x != thing, some_list)

# 4) List comprehension 
some_list = [ x for x in some_list if x != thing ]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #remove #item #list #exists #python
ADD COMMENT
Topic
Name
3+7 =