PythonArraysBeginner

list.clear()

Removes all items from a list in-place (mutates the list).

Review the syntaxStudy the examplesOpen the coding app
my_list.clear()

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Removes all items from a list in-place (mutates the list).

The list.clear() method removes all items from a list object in-place, effectively reducing its length to zero. It is syntactically equivalent to the slice deletion 'del ist[:]'. Unlike assigning a new empty list to a variable (e.g., ist = []), clear() modifies the existing object in memory. This is a critical distinction when multiple variables reference the same list instance; using clear() ensures all references see the empty list, whereas reassignment only affects the local variable. Performance-wise, clear() operates in O(n) time complexity because Python must decrement the reference counts of all elements being removed. It is an efficient way to reset a list while preserving its identity and memory address, which is particularly useful in mutable data structures and long-lived objects.

Quick reference

Syntax

my_list.clear()

See it in practice

Examples

1

Basic List Clearing

fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)
Output:
[]

Removes all elements from the list instance, leaving it empty.

2

Effect on Shared References

original_list = [10, 20, 30]
alias_list = original_list

original_list.clear()

print(f'Original: {original_list}')
print(f'Alias: {alias_list}')
Output:
Original: [] Alias: []

Because clear() mutates the object in-place, all variables pointing to that same object reflect the change.

3

Clear vs. Reassignment Logic

list_a = [1, 2, 3]
list_b = list_a

# Scenario 1: Reassignment
list_a = []
print(f'Reassignment - list_a: {list_a}, list_b: {list_b}')

# Scenario 2: clear()
list_x = [1, 2, 3]
list_y = list_x
list_x.clear()
print(f'clear() - list_x: {list_x}, list_y: {list_y}')
Output:
Reassignment - list_a: [], list_b: [1, 2, 3] clear() - list_x: [], list_y: []

Reassignment points the variable to a new object, while clear() empties the existing object shared by others.

Debug faster

Common Errors

1

AttributeError

Cause: Attempting to call .clear() on an immutable sequence like a tuple.

Fix: Use a list if you need to modify the collection, or create a new empty tuple.

my_data = (1, 2, 3)
my_data.clear()  # Raises AttributeError
2

LogicError

Cause: Assuming clear() returns the emptied list instead of None.

Fix: Do not assign the result of .clear() to a variable; use it as a standalone statement.

items = [5, 10]
items = items.clear()  # items is now None, not []

Runtime support

Compatibility

Python 3.8+

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Removes all items from a list in-place (mutates the list).

AttributeError: Use a list if you need to modify the collection, or create a new empty tuple. LogicError: Do not assign the result of .clear() to a variable; use it as a standalone statement.