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 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)) |
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 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)) |
The original list : ['gfg', 'is', 'best', '', 'for', 'geeks'] Is any string empty in list? : True
Recommended Posts:
- Python | Check whether a list is empty or not
- Python | First Non-Empty String in list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Splitting list on empty string
- Python program to check if string is empty or not
- Python - Ways to remove multiple empty spaces from string List
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python - Remove empty List from List
- Python - Check if string starts with any element in list
- Python | Check if string matches regex list
- Python | Check if suffix matches with any string in given list
- Python: Get List of all empty Directories
- Declare an empty List in Python
- Python | Initialize list with empty dictionaries
- Python - Test for Empty Dictionary Value List
- Python | Remove and print every third from list until it becomes empty
- Python | Remove empty tuples from a list
- Python: Check if a directory is empty
- Python | Check if dictionary is empty
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.

