Python列表 clear() 方法
Python列表clear() 方法用于从列表中删除所有项目。
Syntax: list.clear()
Parameters:
The clear() method doesn’t take any parameters
Returns:
The method doesn’t return any value
示例 1:在 List 中使用 clear() 方法
Python3
# Python program to clear a list
# using clear() method
# Creating list
GEEK = [6, 0, 4, 1]
print('GEEK before clear:', GEEK)
# Clearing list
GEEK.clear()
print('GEEK after clear:', GEEK)
Python3
# Defining a list
list = [{1, 2, 3, 4}, ['1.1', '2.2']]
# clearing the list
list.clear()
print(list)
输出:
GEEK before clear: [6, 0, 4, 1]
GEEK after clear: []
示例 2:
蟒蛇3
# Defining a list
list = [{1, 2, 3, 4}, ['1.1', '2.2']]
# clearing the list
list.clear()
print(list)
输出:
[]