How to Check if All Strings in a Python List are not Empty?

Python – Check if All Strings in List are not Empty

To check if all the strings in a List are not empty, call all() builtin function, and pass the list of strings as argument.

The following program statement is an example to call all() function with list of strings myList as argument.

result = all(myList)

all() returns True if all the strings in the given list of strings are non-empty.

Examples

1. Check if all the elements in given list are non-empty strings

In this example, we will take a list of strings, with three elements in it. All these elements are strings that are non-empty. So, when we call all() function and pass this list as argument, the function should return a boolean value of True.

Python Program

myList = ['a', 'abc', 'cd']
result = all(myList)
print(f'Are all strings non-empty? {result}')
Run Code Copy

Output

Are all strings non-empty? True

2. Negative scenario – One of the string element in list is empty

Let us take a list of strings, such that at least one of them is empty. Now, if we call all() function and pass this list of strings as an argument, then this function should return false. Let us check.

Python Program

myList = ['a', 'abc', 'cd', '']
result = all(myList)
print(f'Are all strings non-empty? : {result}')
Run Code Copy

Output

Are all strings non-empty? : False

Summary

In this tutorial of Python Examples, we learned how to check if all the elements in the list of strings are non-empty.

Related Tutorials

Code copied to clipboard successfully 👍