Python | Check if any String is empty in list (original) (raw)

Last Updated : 04 May, 2023

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

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

print ( "The original list : " + str (test_list))

res = any ( len (ele) = = 0 for ele in test_list)

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

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

print ( "The original list : " + str (test_list))

res = '' in test_list

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*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

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

print ( "The original list : " + str (test_list))

res = False

for i in test_list:

`` if (i.find("") ! = - 1 ):

`` res = True

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

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

print ( "The original list : " + str (test_list))

for i in test_list:

`` if i = = '':

`` print ( "Is any string empty in list? : " , True )

`` 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

import operator as op

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

print ( "The original list : " + str (test_list))

res = bool (op.countOf(test_list, ''))

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

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

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.

Method: Using reduce():
Algorithm:

  1. Create a list of strings named test_list.
  2. Print the original list.
  3. Use the filter() method to create a new list called result that only contains non-empty strings from the test_list.
  4. Compare the length of result with the length of the original test_list.
  5. 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.
  6. 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 ', ' geeks']

print ( "The original list : " + str (test_list))

res = reduce ( lambda x, y: x and y! = '', test_list, True )

print ( "Is any string empty in list? : " + str ( not res))

Output

The original list : ['gfg', 'is', 'best', '', 'for', 'geeks'] 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).