remove

list.clear

Removes all items from the list in place. Equivalent to `del lst[:]`; returns None.

list.clear()

Parameters

Parameter Purpose
(no args) clear() takes no arguments.
vs. lst = [] Rebinding creates a new empty list; other names still see the old one. clear() empties the shared object.

Examples

>>> lst = [1, 2, 3]
>>> lst.clear()
>>> lst
[]

Empties the list in place.

>>> a = [1, 2]; b = a
>>> a.clear()
>>> b
[]

Aliases see the empty list because the same object was mutated.

>>> a = [1, 2]; b = a
>>> a = []
>>> b
[1, 2]

Rebinding (a = []) does NOT clear the shared object.

>>> lst = [1, 2, 3]
>>> del lst[:]
>>> lst
[]

del lst[:] is the pre-3.3 equivalent.

Gotcha

clear() mutates the shared object; `lst = []` merely rebinds the local name — a subtle difference when other references still point at the original list.

Related methods

← All Python list methods · Dict methods · list vs tuple vs set