Open In App

Python – Kth Valid String

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 Kth valid element. Let’s discuss certain ways in which we can find the Kth 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




# Python3 code to demonstrate
# Kth Valid String
# using next() + list comprehension
 
# initializing list
test_list = ["", "", "Akshat", "Nikhil"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
# using next() + list comprehension
# Kth Valid String
test_list = iter(test_list)
for idx in range(0, K):
    res = next(sub for sub in test_list if sub)
 
# printing result
print("The Kth non empty string is : " + str(res))


Output

The original list : ['', '', 'Akshat', 'Nikhil']
The Kth non empty string is : Nikhil

Time Complexity: O(n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”. 

  Method #2 : Using filter() The filter function can be used to find the Non empty strings and the Kth index is returned to get the first string among those. Works only with Python 2. 

Python




# Python code to demonstrate
# Kth Valid String
# using filter()
 
# initializing list
test_list = ["", "", "Akshat", "Nikhil"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
# using filter()
# Kth Valid String
res = filter(None, test_list)[K - 1]
 
# printing result
print("The Kth non empty string is : " + str(res))


Output : 

The original list : ['', '', 'Akshat', 'Nikhil']
The Kth non empty string is : Nikhil

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3: Using list comprehension with enumerate

This method uses list comprehension along with the enumerate function to find the Kth non-empty string. The enumerate function is used to keep track of the index of the non-empty strings in the original list.

Python3




# Python3 code to demonstrate
# Kth Valid String
# using list comprehension with enumerate
   
# initializing list
test_list = ["", "", "Akshat", "Nikhil"]
   
# printing original list
print("The original list : " + str(test_list))
   
# initializing K
K = 2
   
# using list comprehension with enumerate
# Kth Valid String
res = [sub for i, sub in enumerate(test_list) if sub][K-1]
   
# printing result
print("The Kth non empty string is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : ['', '', 'Akshat', 'Nikhil']
The Kth non empty string is : Nikhil

Time Complexity and Auxiliary space are O(n) and O(n) respectively.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads