python中判断列表是否为空

一、

>>> test1 = ["ccc","ddd","mmm"]
>>> if test1:
    for i in test1:
        print(i)
else:
    print("empty list")

    
ccc
ddd
mmm
>>> test1 = []
>>> if test1:
    for i in test1:
        print(i)
else:
    print("empty list")

    
empty list

 

二、

>>> test1 = ["ccc","eee","kkk"]
>>> lenlist=len(test1)
>>> if lenlist != 0:
    for i in test1:
        print(i)
else:
    print("empty list")

    
ccc
eee
kkk
>>> test1 = []
>>> lenlist=len(test1)
>>> if lenlist != 0:
    for i in test1:
        print(i)
else:
    print("empty list")

    
empty list

 

posted @ 2020-12-26 22:00  小鲨鱼2018  阅读(8280)  评论(0编辑  收藏  举报