Python | Check if any String is empty in list

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.