Python 列表为空判断方法 - 3种常用技巧
可以使用以下方法判断 Python 列表是否为空:
- 使用
len()函数:如果列表为空,len(list)的值为 0,可以使用以下代码判断:
if len(list) == 0:
print('List is empty')
- 直接判断列表是否为
[]:如果列表为空,它的值为[],可以使用以下代码判断:
if list == []:
print('List is empty')
- 使用
not操作符:如果列表为空,not操作符会返回True,可以使用以下代码判断:
if not list:
print('List is empty')
原文地址: https://www.cveoy.top/t/topic/lUWE 著作权归作者所有。请勿转载和采集!