Python - 检查列表是否为空
时间:2020-02-23 14:42:29 来源:igfitidea点击:
在本教程中,我们将看到如何检查Python中列表是否为空。
如果列表在Python中是空的,非常容易检查。
我们可以使用"not"来检查python中列表是否为空。
让我们看看简单的例子
list1=[1,2,3]
list2=[]
if not list1:
print("list1 is empty")
else:
print("list1 is not empty")
if not list2:
print("list2 is empty")
else:
print("list2 is not empty")
输出:
list1 is not empty list2 is empty
我们还可以使用Python Len函数来检查列表是否为空。
list3=[]
list4=['one','two','three']
if (len(list3)==0):
print("list3 is empty")
else:
print("list3 is not empty")
if (len(list4)==0):
print("list4 is empty")
else:
print("list4 is not empty")
输出:
list3 is empty list4 is not empty

