Python Tutorial (33) – Example: Clearing a list

Time: Column:Python views:222

Define a List and Clear It Using the clear() Method

The following Python code demonstrates how to clear a list using the clear() method.

Example 1

RUNOOB = [6, 0, 4, 1]
print('Before clearing:', RUNOOB)

# Clear the list
RUNOOB.clear()
print('After clearing:', RUNOOB)

Output:

Before clearing: [6, 0, 4, 1]
After clearing: []

Explanation:

  • Before clearing: The list RUNOOB contains the elements [6, 0, 4, 1].

  • After clearing: The clear() method removes all elements from the list, resulting in an empty list [].