Python | First Non-Empty String in list
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 code to demonstrate # First Non-Empty String in list # using next() + list comprehension # initializing list test_list = ["", "", "Akshat", "Nikhil"] # printing original list print("The original list : " + str(test_list)) # using next() + list comprehension # First Non-Empty String in list res = next(sub for sub in test_list if sub) # printing result print("The first non empty string is : " + str(res)) |
The original list : ['', '', 'Akshat', 'Nikhil'] The first non empty string is : Akshat
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 code to demonstrate # First Non-Empty String in list # using filter() # initializing list test_list = ["", "", "Akshat", "Nikhil"] # printing original list print("The original list : " + str(test_list)) # using filter() # First Non-Empty String in list res = filter(None, test_list)[0] # printing result print("The first non empty string is : " + str(res)) |
The original list : ['', '', 'Akshat', 'Nikhil'] The first non empty string is : Akshat
Recommended Posts:
- Python | Convert list of string to list of list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert string enclosed list to list
- Python | Convert a string representation of list into list
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Sum of list (with string types) in Python
- Python | Get the substring from given string using list slicing
- Python | Insert value after each k letters in given list of string
- Python | Program to convert String to a List
- Python | Test if string contains element from list
- Python | Split string into list of characters
- Python | Categorize the given list by string size
- Python | Splitting string to list of characters
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.



