Search
 
SCRIPT & CODE EXAMPLE
 

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 ]
Comment

PREVIOUS NEXT
Code Example
Python :: python execute file 
Python :: add something to list python 
Python :: how to slice dataframe based on daterange in pandas 
Python :: encode labels in scikit learn 
Python :: execute python in notepad++ 
Python :: how to sort a list in python using lambda 
Python :: how to check if index is out of range python 
Python :: mongodb group by having 
Python :: remove blanks from list python 
Python :: get cuda memory pytorch 
Python :: drop row based on NaN value of a column 
Python :: How to Add R to Jupyter Notebook 
Python :: compute mad python 
Python :: sort defaultdict by value 
Python :: python exec return value 
Python :: how to format integer to two digit in python 
Python :: pytorch use multiple gpu 
Python :: remove a character from a string python 
Python :: python insert object into list 
Python :: tkinter window background color 
Python :: get date and time formatted python 
Python :: python trace table generator 
Python :: how to open sound file in python 
Python :: python regex find first 
Python :: time.ctime(os.path.getmtime phyton in datetime 
Python :: how to remove in null values in pandas 
Python :: sending email in django 
Python :: colab add library 
Python :: how to create a new virtualenv 
Python :: initialize dictionary with empty lists 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =