Remove item from list if it exists python

Code examples

0
0

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 ]

Similar pages

Similar pages with examples

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................