How do I check if a list is empty?

2018-12-31 04:46发布

For example, if passed the following:

a = []

How do I check to see if a is empty?

标签: python list
30条回答
一个人的天荒地老
2楼-- · 2018-12-31 04:53

I had written:

if isinstance(a, (list, some, other, types, i, accept)) and not a:
    do_stuff

which was voted -1. I'm not sure if that's because readers objected to the strategy or thought the answer wasn't helpful as presented. I'll pretend it was the latter, since---whatever counts as "pythonic"---this is the correct strategy. Unless you've already ruled out, or are prepared to handle cases where a is, for example, False, you need a test more restrictive than just if not a:. You could use something like this:

if isinstance(a, numpy.ndarray) and not a.size:
    do_stuff
elif isinstance(a, collections.Sized) and not a:
    do_stuff

the first test is in response to @Mike's answer, above. The third line could also be replaced with:

elif isinstance(a, (list, tuple)) and not a:

if you only want to accept instances of particular types (and their subtypes), or with:

elif isinstance(a, (list, tuple)) and not len(a):

You can get away without the explicit type check, but only if the surrounding context already assures you that a is a value of the types you're prepared to handle, or if you're sure that types you're not prepared to handle are going to raise errors (e.g., a TypeError if you call len on a value for which it's undefined) that you're prepared to handle. In general, the "pythonic" conventions seem to go this last way. Squeeze it like a duck and let it raise a DuckError if it doesn't know how to quack. You still have to think about what type assumptions you're making, though, and whether the cases you're not prepared to handle properly really are going to error out in the right places. The Numpy arrays are a good example where just blindly relying on len or the boolean typecast may not do precisely what you're expecting.

查看更多
只靠听说
3楼-- · 2018-12-31 04:53

Simple way is checking the length is equal zero.

if len(a) == 0:
    print("a is empty")
查看更多
笑指拈花
4楼-- · 2018-12-31 04:54

Another simple way could be

a = []
if len(a) == 0:
  print("Empty")
else:
  print(" Not empty")
查看更多
孤独总比滥情好
5楼-- · 2018-12-31 04:54

Look at the following code executed on Python interactive terminal.

>>> a = []
>>> if a:
...     print "List is not empty";
... else:
...     print "List is empty"
... 
List is empty
>>> 
>>> a = [1, 4, 9]
>>> if a:
...     print "List is not empty";
... else:
...     print "List is empty"
... 
List is not empty
>>> 
查看更多
浮光初槿花落
6楼-- · 2018-12-31 04:57

The pythonic way to do it is from the PEP 8 style guide (where Yes means “recommended” and No means “not recommended”):

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):
查看更多
柔情千种
7楼-- · 2018-12-31 04:59

You can check if the length of the array is zero (or not.) If the length of the array is zero, then it is empty. try the following:

a = []
if len(a)==0 : print "List is empty"
查看更多
登录 后发表回答