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 05:14
def list_test (L):
    if   L is None  : print 'list is None'
    elif not L      : print 'list is empty'
    else: print 'list has %d elements' % len(L)

list_test(None)
list_test([])
list_test([1,2,3])

It is sometimes good to test for None and for emptiness separately as those are two different states. The code above produces the following output:

list is None 
list is empty 
list has 3 elements

Although it's worth nothing that None is falsy. So if you don't want to separate test for None-ness, you don't have to do that.

def list_test2 (L):
    if not L      : print 'list is empty'
    else: print 'list has %d elements' % len(L)

list_test2(None)
list_test2([])
list_test2([1,2,3])

produces expected

list is empty
list is empty
list has 3 elements
查看更多
若你有天会懂
3楼-- · 2018-12-31 05:15

I prefer it explicitly:

if len(li) == 0:
    print('the list is empty')

This way it's 100% clear that li is a sequence (list) and we want to test its size. My problem with if not li: ... is that it gives the false impression that li is a boolean variable.

查看更多
步步皆殇っ
4楼-- · 2018-12-31 05:15

Python is very uniform about the treatment of emptiness. Given the following:

a = []

.
.
.

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

You simply check list a with an "if" statement to see if it is empty. From what I have read and been taught, this is the "Pythonic" way to see if a list or tuple is empty.

查看更多
浪荡孟婆
5楼-- · 2018-12-31 05:15

To check whether a list is empty or not you can use two following ways. But remember, we should avoid the way of explicitly checking for a sequence or list (it's a less pythonic way):

def Enquiry(list1): 
    if len(list1) == 0: 
        return 0
    else: 
        return 1

# ––––––––––––––––––––––––––––––––

list1 = [] 

if Enquiry(list1): 
    print ("The list isn't empty") 
else: 
    print("The list is Empty") 

# Result: "The list is Empty".

The second way is a more pythonic one. This method is an implicit way of checking and much more preferable than the previous one.

def Enquiry(list1): 
    if not list1: 
        return 1
    else: 
        return 0

# ––––––––––––––––––––––––––––––––

list1 = [] 

if Enquiry(list1): 
    print ("The list is Empty") 
else: 
    print ("The list isn't empty") 

# Result: "The list is Empty"

Hope this helps.

查看更多
孤独寂梦人
6楼-- · 2018-12-31 05:17

If you want to check if list is empty;

l = []
if l:
    # do your stuff.

If you want to check weather all the values in list is empty.

l = ["", False, 0, '', [], {}, ()]
if all(bool(x) for x in l):
    # do your stuff.

However this will be True for empty list.

def empty_list(lst):
    if len(lst) ==0:
        return false
    else:
        return all(bool(x) for x in l)

Now you can use:

if empty_list(lst):
    # do your stuff.
查看更多
美炸的是我
7楼-- · 2018-12-31 05:20

Some methods that I use:

if not a:
    print "list is empty"


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