Python | First Non-Empty String in list
Last Updated :
15 May, 2023
Sometimes while dealing with data science, we need to handle a large amount of data and hence we may require shorthands to perform certain tasks. We handle the Null values at preprocessing stage and hence sometimes require to check for the 1st valid element. Let’s discuss certain ways in which we can find the first Non-Empty String.
Method #1 : Using next() + list comprehension The next function returns the iterator and hence its more efficient that conventional list comprehension and the logic part is handled using list comprehension which checks for the last None value.
Python3
test_list = [" ", " ", " Akshat ", " Nikhil"]
print ( "The original list : " + str (test_list))
res = next (sub for sub in test_list if sub)
print ( "The first non empty string is : " + str (res))
|
Output :
The original list : ['', '', 'Akshat', 'Nikhil']
The first non empty string is : Akshat
Time Complexity: O(n) , where n is the number of elements in the list.
Auxiliary Space: O(m), where m is the length of the first non-empty string.
Method #2 : Using filter() The filter function can be used to find the Non empty strings and the 0th index is returned to get the first string among those. Works only with Python 2.
Python
test_list = [" ", " ", " Akshat ", " Nikhil"]
print ( "The original list : " + str (test_list))
res = filter ( None , test_list)[ 0 ]
print ( "The first non empty string is : " + str (res))
|
Output :
The original list : ['', '', 'Akshat', 'Nikhil']
The first non empty string is : Akshat
Time Complexity: O(n) , where n is the number of elements in the list.
Auxiliary Space: O(m), where m is the length of the first non-empty string.
Method #3: Using len() method.If the length of string is zero then string is empty otherwise it is not empty.
Python3
test_list = [" ", " ", " Akshat ", " Nikhil"]
print ( "The original list : " + str (test_list))
res = ""
for i in test_list:
if ( len (i)! = 0 ):
res = i
break
print ( "The first non empty string is : " + str (res))
|
Output
The original list : ['', '', 'Akshat', 'Nikhil']
The first non empty string is : Akshat
Time Complexity: O(n) , where n is the number of elements in the list.
Auxiliary Space: O(m), where m is the length of the first non-empty string.
Method #4: Using the reduce function: You can use the reduce function from the functools module to find the first non-empty string in the list. The reduce function applies a given function to the elements of the list, starting from the left, and returns a single value.
Python3
from functools import reduce
test_list = [" ", " ", " Akshat ", " Nikhil"]
res = reduce ( lambda s1, s2: s1 or s2, test_list)
print ( "The first non-empty string:" , res)
|
Output
The first non-empty string: Akshat
Time complexity: O(n), where n is the length of the list.
Auxiliary Space: O(1)
Method #5 : Using isspace() method
Python3
test_list = [" ", " ", " Akshat ", " Nikhil"]
print ( "The original list : " + str (test_list))
res = ""
for i in test_list:
x = i + " "
if ( not x.isspace()):
res = i
break
print ( "The first non empty string is : " + str (res))
|
Output
The original list : ['', '', 'Akshat', 'Nikhil']
The first non empty string is : Akshat
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #6: Using numpy:
Algorithm:
- Initialize the list test_list.
- Convert the list to a NumPy array arr.
- Use the nonzero() function on the array arr to get the indices of the non-zero elements.
- Get the first element from the indices using [0][0] because the nonzero() function returns a tuple of arrays,
- one for each dimension of the input array, and we are working with a 1-dimensional array.
- Get the first non-empty string from the arr array using the index obtained in step 4.
- Print the result.
Python3
import numpy as np
test_list = [" ", " ", " Akshat ", " Nikhil"]
print ( "The original list : " + str (test_list))
arr = np.array(test_list)
idx = np.nonzero(arr)[ 0 ][ 0 ]
res = arr[idx]
print ( "The first non-empty string:" , res)
|
Output:
The original list : ['', '', 'Akshat', 'Nikhil']
The first non-empty string: Akshat
Time complexity: O(n), where n is the length of the input list test_list. The nonzero() function in NumPy takes linear time to find the indices of the non-zero elements.
Space complexity: O(n), where n is the length of the input list test_list. The NumPy array arr created in this code will have the same size as the input list, and hence the space complexity is O(n).
Share your thoughts in the comments
Please Login to comment...