如何检查列表是否为空?


例如,如果通过以下:

a = []

如何检查a是否为空?

if not a:
  print("List is empty")

使用空列表的隐式布尔型是相当pythonic。

Pythonic的方式是从 PEP 8风格指南

For sequences, (strings, lists, tuples), use the fact that empty sequences
are false.

**Yes:** if not seq:

     if seq:

**No:**  if len(seq):

     if not len(seq):

>

未经作者同意,本文严禁转载,违者必究!