Python | Kth Non-None String from Rear

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 from rear. Let’s discuss certain ways in which we can find the Kth Non-Empty String from rear.

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. The rear part is handled by reversing the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Kth Non-None String from Rear
# 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 Non-None String from Rear
test_list.reverse()
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 from rear is : " + str(res))

chevron_right


Output :

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

 

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 last Kth string among those. Works only with Python 2.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# Kth Non-None String from Rear
# 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 Non-None String from Rear
res = filter(None, test_list)[-K]
  
# printing result
print("The Kth non empty string from rear is : " + str(res))

chevron_right


Output :

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.