English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic Python tutorial

Flow control in Python

Fonctions en Python

Types de données en Python

File operations in Python

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Usage and examples of Python list clear()

List methods in Python

The clear() method removes all items from the list.

The syntax of the clear() method is:

list.clear()

Parameters of clear()

The clear() method does not take any parameters.

The return value of clear()

The clear() method only clears the givenlist. It does not return any value.

Example1: The operation of the clear() method

# Define a list
list = [{1, 2}, ('a'), ['1.1', ''2.2']]
# Clear the list
list.clear()
print('List:', list)

When running the program, the output is:

List: []

Note:If you are using Python 2or Python 3.2and lower versions, the clear() method cannot be used. You can use the del operator instead.

Example2: Use del to clear the list

# Define a list
list = [{1, 2}, ('a'), ['1.1', ''2.2']]
# Clear the list
del list[:]
print('List:', list)

List methods in Python