如何在 Python 中检查列表是否为空?

我使用的 API 可以返回空的 []列表。

下面的条件语句没有按照预期的方式工作:

if myList is not None: #not working
pass


if myList is not []: #not working
pass

什么有用?

227406 次浏览
if not myList:
print "Nothing here"

在布尔上下文(如 if some_list:)中,空列表的计算结果为 False。

我喜欢扎伦比斯蒂的回答。不过,如果你想更明确地说,你可以这样做:

if len(my_list) == 0:
print "my_list is empty"