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 ', ' Lazyroar'] # 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', 'Lazyroar'] Is any string empty in list? : True
Time Complexity: O(n), where n is the length of the input list. This is because we’re using any() + len() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space.
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 ', ' Lazyroar'] # 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', 'Lazyroar'] Is any string empty in list? : True
Time complexity: O(n*n), where n is the length of the numbers list. The in operator has a time complexity of O(n)
Auxiliary Space: O(n), as we create new list res where n is the length of the numbers list.
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 ', ' Lazyroar'] # 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)) |
The original list : ['gfg', 'is', 'best', '', 'for', 'Lazyroar'] Is any string empty in list? : True
Method: Using list comprehension
Python3
test_list = [ 'gfg' , 'is' , 'best' , ' ', ' for ', ' Lazyroar'] x = [i for i in test_list if i ! = ''] print (x) |
['gfg', 'is', 'best', 'for', 'Lazyroar']
Method: Using enumerate function
Python3
test_list = [ 'gfg' , 'is' , 'best' , ' ', ' for ', ' Lazyroar'] x = [i for j,i in enumerate (test_list) if i ! = ''] print (x) |
['gfg', 'is', 'best', 'for', 'Lazyroar']
Method: Using ‘==’ operator
Python3
# checking empty strings in the list using '==' test_list = [ 'gfg' , 'is' , 'best' , ' ', ' for ', ' Lazyroar'] 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 |
The original list : ['gfg', 'is', 'best', '', 'for', 'Lazyroar'] 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 ', ' Lazyroar'] # 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)) |
The original list : ['gfg', 'is', 'best', '', 'for', 'Lazyroar'] 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 ', ' Lazyroar'] 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 |
The original list : ['gfg', 'is', 'best', '', 'for', 'Lazyroar'] 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.
Method: Using reduce():
Algorithm:
- Create a list of strings named test_list.
- Print the original list.
- Use the filter() method to create a new list called result that only contains non-empty strings from the test_list.
- Compare the length of result with the length of the original test_list.
- If the lengths are not equal, then there is at least one empty string in the original test_list, so set res to True. Otherwise, set res to False.
- Print whether or not there is any empty string in the original test_list.
Python3
from functools import reduce test_list = [ 'gfg' , 'is' , 'best' , ' ', ' for ', ' Lazyroar'] print ( "The original list : " + str (test_list)) # Check if any string is empty in list res = reduce ( lambda x, y: x and y! = '', test_list, True ) print ( "Is any string empty in list? : " + str ( not res)) #This code is contributed by Jyothi pinjala. |
The original list : ['gfg', 'is', 'best', '', 'for', 'Lazyroar'] Is any string empty in list? : True
Time complexity:
The time complexity of this code depends on the length of the input list test_list. The filter() method has a time complexity of O(n), where n is the length of the input list. The length comparison takes constant time, so it does not affect the overall time complexity. Therefore, the time complexity of this code is O(n).
Space complexity:
The space complexity of this code also depends on the length of the input list test_list. The result list created by the filter() method could potentially be the same length as the original test_list, so the space complexity is O(n) in the worst case. The other variables created (such as res) take constant space, so they do not affect the overall space complexity. Therefore, the space complexity of this code is also O(n).