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
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14194621.html