Python Glossary Русский

Python 3.3+Lists (list)function

list.clear

removal

Remove all items, leaving an empty list (in place).

Syntax

lst.clear()

The difference from lst = [] matters: clear() empties the object itself, so every other name bound to that list sees it become empty, while assignment merely rebinds one name to a fresh list and leaves the old one untouched. del lst[:] does exactly the same thing — clear() is just more readable. The method exists since Python 3.3.

Examples

lst = [1, 2, 3]
lst.clear()
print(lst)        # → []
print(len(lst))   # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy