Python Glossary Русский

all Python 3Lists (list)function

list.remove

removal

Remove the first matching value; raises ValueError if not found.

Syntax

lst.remove(value)

Matching uses ==, not identity, so lst.remove(1) will happily drop a True, and lst.remove(1.0) will drop an integer 1. Only the first match goes, and the scan is linear, so stripping every copy with a while loop around remove is both slow and error-prone — build a filtered list with a comprehension instead. Never remove items while iterating the same list with for: indices shift and some elements get skipped entirely.

Examples

lst = [1, 2, 3, 2]
lst.remove(2)
print(lst)   # → [1, 3, 2]
lst.remove(2)
print(lst)   # → [1, 3]

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy