Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Check if any String is empty in list

Improve Article
Save Article
Like Article
  • Last Updated : 20 Mar, 2023
Improve Article
Save Article
Like Article

Sometimes, while working with Python, we can have a problem in which we need to check for perfection of data in list. One of parameter can be that each element in list is non-empty. Let’s discuss if a list is perfect on this factor using certain methods. 

Method #1 : Using any() + len() The combination of above functions can be used to perform this task. In this, we use any() to check for any element in string and len() is used to get if length of any string is 0 or not. 

Python3




# Python3 code to demonstrate working of
# Check if any String is empty in list
# using len() + any()
 
# initialize list
test_list = ['gfg', 'is', 'best', '', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# Check if any String is empty in list
# using len() + any()
res = any(len(ele) == 0 for ele in test_list)
 
# printing result
print("Is any string empty in list? : " + str(res))

Output

The original list : ['gfg', 'is', 'best', '', 'for', 'geeks']
Is any string empty in list? : True

  Method #2 : Using in operator This task can also be performed using in operator. This checks internally for all strings in list and returns True if we find any empty string. 

Python3




# Python3 code to demonstrate working of
# Check if any String is empty in list
# using in operator
 
# initialize list
test_list = ['gfg', 'is', 'best', '', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# Check if any String is empty in list
# using in operator
res = '' in test_list
 
# printing result
print("Is any string empty in list? : " + str(res))

Output

The original list : ['gfg', 'is', 'best', '', 'for', 'geeks']
Is any string empty in list? : True

Method #3 : Using find()

Python3




# Python3 code to demonstrate working of
# Check if any String is empty in list
 
# initialize list
test_list = ['gfg', 'is', 'best', '', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# Check if any String is empty in list
res = False
for i in test_list:
    if(i.find("") != -1):
        res = True
# printing result
print("Is any string empty in list? : " + str(res))

Output

The original list : ['gfg', 'is', 'best', '', 'for', 'geeks']
Is any string empty in list? : True

Method: Using list comprehension 

Python3




test_list = ['gfg', 'is', 'best', '', 'for', 'geeks']
x=[i for i in test_list     if i != '']
print(x)

Output

['gfg', 'is', 'best', 'for', 'geeks']

Method: Using enumerate function 

Python3




test_list = ['gfg', 'is', 'best', '', 'for', 'geeks']
x=[i for j,i in enumerate(test_list)     if i != '']
print(x)

Output

['gfg', 'is', 'best', 'for', 'geeks']

Method: Using ‘==’ operator 

Python3




# checking empty strings in the list using '=='
test_list=['gfg', 'is', 'best', '', 'for', 'geeks']
print("The original list : " + str(test_list))
# loop to get every string in the list
for i in test_list:
  # checking string empty or not
    if i=='':
        print("Is any string empty in list? : ",True)
        #loops stops after finding empty string
        break

Output

The original list : ['gfg', 'is', 'best', '', 'for', 'geeks']
Is any string empty in list? :  True

Time complexity: O(n)

Auxiliary space: O(n), where n is length of list

Method: Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Check if any String is empty in list
import operator as op
 
# initialize list
test_list = ['gfg', 'is', 'best', '', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# Check if any String is empty in list
res = bool(op.countOf(test_list, ''))
 
# printing result
print("Is any string empty in list? : " + str(res))

Output

The original list : ['gfg', 'is', 'best', '', 'for', 'geeks']
Is any string empty in list? : True

Time Complexity: O(N)

Auxiliary Space: O(1)

Method: Using filter function
 

Python3




test_list = ['gfg', 'is', 'best', '', 'for', 'geeks']
print("The original list : " + str(test_list))
 
#Check if any String is empty in list
result = list(filter(lambda x: x!='', test_list))
if len(result) != len(test_list):
  res = True
else:
  res = False
print("Is any string empty in list? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original list : ['gfg', 'is', 'best', '', 'for', 'geeks']
Is any string empty in list? : True

Time Complexity: O(n)
Auxiliary Space: O(n)
In this method, we use the filter function which returns an iterator yielding only those elements from the input list for which the lambda function returns True. The lambda function checks for equality of each element with an empty string and returns True only if it is not equal. If the length of filtered list and the original list are unequal, it means that there was an empty string in the original list and the function returns True.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!