Python 3 reference · List Methods
Python List clear() Method
Remove every item from the list in place, leaving an empty list. Other references see the change too.
Python 3 Since 3.0
Syntax
list.clear()
Return value
None.
Examples
cart = ["tea", "cake"]
cart.clear()
print(cart, len(cart))
Output
[] 0
a = [1, 2, 3]
b = a # same list
a.clear()
print(b)
c = [1, 2, 3]
d = c
c = [] # new list; d is unchanged
print(d)
Output
[] [1, 2, 3]
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
list.pop()· Remove and return the item at a position, the last item by default. Raises IndexError on an empty list.list.remove()· Remove the first item equal to a value. Raises ValueError if the value is not in the list.dict.clear()· Remove every key and value from the dictionary in place, leaving it empty. Other references see the change too.- Python Lists · tutorial