Python 列表 clear() 方法用於從列表。
用法:list.clear()
參數:
clear() 方法不帶任何參數
返回值:
該方法不返回任何值
示例 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)
輸出:
GEEK before clear:[6, 0, 4, 1] GEEK after clear:[]
範例2:
Python3
# Defining a list
list = [{1, 2, 3, 4}, ['1.1', '2.2']]
# clearing the list
list.clear()
print(list)
輸出:
[]
相關用法
- Selenium Python clear()用法及代碼示例
- Python set clear()用法及代碼示例
- Python Dictionary clear()用法及代碼示例
- Python Matplotlib.axes.Axes.clear()用法及代碼示例
- Python Matplotlib.figure.Figure.clear()用法及代碼示例
- Python turtle.clear()用法及代碼示例
注:本文由純淨天空篩選整理自AmiyaRanjanRout大神的英文原創作品 Python List clear() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。